hfc_usb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. /*
  2. * hfc_usb.c
  3. *
  4. * $Id: hfc_usb.c,v 2.3.2.24 2007/10/14 08:40:29 mbachem Exp $
  5. *
  6. * modular HiSax ISDN driver for Colognechip HFC-S USB chip
  7. *
  8. * Authors : Peter Sprenger (sprenger@moving-bytes.de)
  9. * Martin Bachem (m.bachem@gmx.de, info@colognechip.com)
  10. *
  11. * based on the first hfc_usb driver of
  12. * Werner Cornelius (werner@isdn-development.de)
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * See Version Histroy at the bottom of this file
  29. *
  30. */
  31. #include <linux/types.h>
  32. #include <linux/stddef.h>
  33. #include <linux/timer.h>
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/kernel_stat.h>
  37. #include <linux/usb.h>
  38. #include <linux/kernel.h>
  39. #include <linux/sched.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/slab.h>
  42. #include "hisax.h"
  43. #include "hisax_if.h"
  44. #include "hfc_usb.h"
  45. static const char *hfcusb_revision =
  46. "$Revision: 2.3.2.24 $ $Date: 2007/10/14 08:40:29 $ ";
  47. /* Hisax debug support
  48. * debug flags defined in hfc_usb.h as HFCUSB_DBG_[*]
  49. */
  50. #define __debug_variable hfc_debug
  51. #include "hisax_debug.h"
  52. static u_int debug;
  53. module_param(debug, uint, 0);
  54. static int hfc_debug;
  55. /* private vendor specific data */
  56. typedef struct {
  57. __u8 led_scheme; // led display scheme
  58. signed short led_bits[8]; // array of 8 possible LED bitmask settings
  59. char *vend_name; // device name
  60. } hfcsusb_vdata;
  61. /* VID/PID device list */
  62. static const struct usb_device_id hfcusb_idtab[] = {
  63. {
  64. USB_DEVICE(0x0959, 0x2bd0),
  65. .driver_info = (unsigned long) &((hfcsusb_vdata)
  66. {LED_OFF, {4, 0, 2, 1},
  67. "ISDN USB TA (Cologne Chip HFC-S USB based)"}),
  68. },
  69. {
  70. USB_DEVICE(0x0675, 0x1688),
  71. .driver_info = (unsigned long) &((hfcsusb_vdata)
  72. {LED_SCHEME1, {1, 2, 0, 0},
  73. "DrayTek miniVigor 128 USB ISDN TA"}),
  74. },
  75. {
  76. USB_DEVICE(0x07b0, 0x0007),
  77. .driver_info = (unsigned long) &((hfcsusb_vdata)
  78. {LED_SCHEME1, {0x80, -64, -32, -16},
  79. "Billion tiny USB ISDN TA 128"}),
  80. },
  81. {
  82. USB_DEVICE(0x0742, 0x2008),
  83. .driver_info = (unsigned long) &((hfcsusb_vdata)
  84. {LED_SCHEME1, {4, 0, 2, 1},
  85. "Stollmann USB TA"}),
  86. },
  87. {
  88. USB_DEVICE(0x0742, 0x2009),
  89. .driver_info = (unsigned long) &((hfcsusb_vdata)
  90. {LED_SCHEME1, {4, 0, 2, 1},
  91. "Aceex USB ISDN TA"}),
  92. },
  93. {
  94. USB_DEVICE(0x0742, 0x200A),
  95. .driver_info = (unsigned long) &((hfcsusb_vdata)
  96. {LED_SCHEME1, {4, 0, 2, 1},
  97. "OEM USB ISDN TA"}),
  98. },
  99. {
  100. USB_DEVICE(0x08e3, 0x0301),
  101. .driver_info = (unsigned long) &((hfcsusb_vdata)
  102. {LED_SCHEME1, {2, 0, 1, 4},
  103. "Olitec USB RNIS"}),
  104. },
  105. {
  106. USB_DEVICE(0x07fa, 0x0846),
  107. .driver_info = (unsigned long) &((hfcsusb_vdata)
  108. {LED_SCHEME1, {0x80, -64, -32, -16},
  109. "Bewan Modem RNIS USB"}),
  110. },
  111. {
  112. USB_DEVICE(0x07fa, 0x0847),
  113. .driver_info = (unsigned long) &((hfcsusb_vdata)
  114. {LED_SCHEME1, {0x80, -64, -32, -16},
  115. "Djinn Numeris USB"}),
  116. },
  117. {
  118. USB_DEVICE(0x07b0, 0x0006),
  119. .driver_info = (unsigned long) &((hfcsusb_vdata)
  120. {LED_SCHEME1, {0x80, -64, -32, -16},
  121. "Twister ISDN TA"}),
  122. },
  123. {
  124. USB_DEVICE(0x071d, 0x1005),
  125. .driver_info = (unsigned long) &((hfcsusb_vdata)
  126. {LED_SCHEME1, {0x02, 0, 0x01, 0x04},
  127. "Eicon DIVA USB 4.0"}),
  128. },
  129. { }
  130. };
  131. /* structure defining input+output fifos (interrupt/bulk mode) */
  132. struct usb_fifo; /* forward definition */
  133. typedef struct iso_urb_struct {
  134. struct urb *purb;
  135. __u8 buffer[ISO_BUFFER_SIZE]; /* buffer incoming/outgoing data */
  136. struct usb_fifo *owner_fifo; /* pointer to owner fifo */
  137. } iso_urb_struct;
  138. struct hfcusb_data; /* forward definition */
  139. typedef struct usb_fifo {
  140. int fifonum; /* fifo index attached to this structure */
  141. int active; /* fifo is currently active */
  142. struct hfcusb_data *hfc; /* pointer to main structure */
  143. int pipe; /* address of endpoint */
  144. __u8 usb_packet_maxlen; /* maximum length for usb transfer */
  145. unsigned int max_size; /* maximum size of receive/send packet */
  146. __u8 intervall; /* interrupt interval */
  147. struct sk_buff *skbuff; /* actual used buffer */
  148. struct urb *urb; /* transfer structure for usb routines */
  149. __u8 buffer[128]; /* buffer incoming/outgoing data */
  150. int bit_line; /* how much bits are in the fifo? */
  151. volatile __u8 usb_transfer_mode; /* switched between ISO and INT */
  152. iso_urb_struct iso[2]; /* need two urbs to have one always for pending */
  153. struct hisax_if *hif; /* hisax interface */
  154. int delete_flg; /* only delete skbuff once */
  155. int last_urblen; /* remember length of last packet */
  156. } usb_fifo;
  157. /* structure holding all data for one device */
  158. typedef struct hfcusb_data {
  159. /* HiSax Interface for loadable Layer1 drivers */
  160. struct hisax_d_if d_if; /* see hisax_if.h */
  161. struct hisax_b_if b_if[2]; /* see hisax_if.h */
  162. int protocol;
  163. struct usb_device *dev; /* our device */
  164. int if_used; /* used interface number */
  165. int alt_used; /* used alternate config */
  166. int ctrl_paksize; /* control pipe packet size */
  167. int ctrl_in_pipe, /* handles for control pipe */
  168. ctrl_out_pipe;
  169. int cfg_used; /* configuration index used */
  170. int vend_idx; /* vendor found */
  171. int b_mode[2]; /* B-channel mode */
  172. int l1_activated; /* layer 1 activated */
  173. int disc_flag; /* TRUE if device was disonnected to avoid some USB actions */
  174. int packet_size, iso_packet_size;
  175. /* control pipe background handling */
  176. ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE]; /* buffer holding queued data */
  177. volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt; /* input/output pointer + count */
  178. struct urb *ctrl_urb; /* transfer structure for control channel */
  179. struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
  180. struct usb_ctrlrequest ctrl_read; /* same for read request */
  181. __u8 old_led_state, led_state;
  182. volatile __u8 threshold_mask; /* threshold actually reported */
  183. volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
  184. usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
  185. volatile __u8 l1_state; /* actual l1 state */
  186. struct timer_list t3_timer; /* timer 3 for activation/deactivation */
  187. struct timer_list t4_timer; /* timer 4 for activation/deactivation */
  188. } hfcusb_data;
  189. static void collect_rx_frame(usb_fifo *fifo, __u8 *data, int len,
  190. int finish);
  191. static inline const char *
  192. symbolic(struct hfcusb_symbolic_list list[], const int num)
  193. {
  194. int i;
  195. for (i = 0; list[i].name != NULL; i++)
  196. if (list[i].num == num)
  197. return (list[i].name);
  198. return "<unknown ERROR>";
  199. }
  200. static void
  201. ctrl_start_transfer(hfcusb_data *hfc)
  202. {
  203. if (hfc->ctrl_cnt) {
  204. hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
  205. hfc->ctrl_urb->setup_packet = (u_char *)&hfc->ctrl_write;
  206. hfc->ctrl_urb->transfer_buffer = NULL;
  207. hfc->ctrl_urb->transfer_buffer_length = 0;
  208. hfc->ctrl_write.wIndex =
  209. cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
  210. hfc->ctrl_write.wValue =
  211. cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
  212. usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
  213. }
  214. } /* ctrl_start_transfer */
  215. static int
  216. queue_control_request(hfcusb_data *hfc, __u8 reg, __u8 val, int action)
  217. {
  218. ctrl_buft *buf;
  219. if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
  220. return (1); /* no space left */
  221. buf = &hfc->ctrl_buff[hfc->ctrl_in_idx]; /* pointer to new index */
  222. buf->hfc_reg = reg;
  223. buf->reg_val = val;
  224. buf->action = action;
  225. if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
  226. hfc->ctrl_in_idx = 0; /* pointer wrap */
  227. if (++hfc->ctrl_cnt == 1)
  228. ctrl_start_transfer(hfc);
  229. return (0);
  230. }
  231. static void
  232. ctrl_complete(struct urb *urb)
  233. {
  234. hfcusb_data *hfc = (hfcusb_data *) urb->context;
  235. urb->dev = hfc->dev;
  236. if (hfc->ctrl_cnt) {
  237. hfc->ctrl_cnt--; /* decrement actual count */
  238. if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
  239. hfc->ctrl_out_idx = 0; /* pointer wrap */
  240. ctrl_start_transfer(hfc); /* start next transfer */
  241. }
  242. }
  243. /* write led data to auxport & invert if necessary */
  244. static void
  245. write_led(hfcusb_data *hfc, __u8 led_state)
  246. {
  247. if (led_state != hfc->old_led_state) {
  248. hfc->old_led_state = led_state;
  249. queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
  250. }
  251. }
  252. static void
  253. set_led_bit(hfcusb_data *hfc, signed short led_bits, int on)
  254. {
  255. if (on) {
  256. if (led_bits < 0)
  257. hfc->led_state &= ~abs(led_bits);
  258. else
  259. hfc->led_state |= led_bits;
  260. } else {
  261. if (led_bits < 0)
  262. hfc->led_state |= abs(led_bits);
  263. else
  264. hfc->led_state &= ~led_bits;
  265. }
  266. }
  267. /* handle LED requests */
  268. static void
  269. handle_led(hfcusb_data *hfc, int event)
  270. {
  271. hfcsusb_vdata *driver_info =
  272. (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
  273. /* if no scheme -> no LED action */
  274. if (driver_info->led_scheme == LED_OFF)
  275. return;
  276. switch (event) {
  277. case LED_POWER_ON:
  278. set_led_bit(hfc, driver_info->led_bits[0], 1);
  279. set_led_bit(hfc, driver_info->led_bits[1], 0);
  280. set_led_bit(hfc, driver_info->led_bits[2], 0);
  281. set_led_bit(hfc, driver_info->led_bits[3], 0);
  282. break;
  283. case LED_POWER_OFF:
  284. set_led_bit(hfc, driver_info->led_bits[0], 0);
  285. set_led_bit(hfc, driver_info->led_bits[1], 0);
  286. set_led_bit(hfc, driver_info->led_bits[2], 0);
  287. set_led_bit(hfc, driver_info->led_bits[3], 0);
  288. break;
  289. case LED_S0_ON:
  290. set_led_bit(hfc, driver_info->led_bits[1], 1);
  291. break;
  292. case LED_S0_OFF:
  293. set_led_bit(hfc, driver_info->led_bits[1], 0);
  294. break;
  295. case LED_B1_ON:
  296. set_led_bit(hfc, driver_info->led_bits[2], 1);
  297. break;
  298. case LED_B1_OFF:
  299. set_led_bit(hfc, driver_info->led_bits[2], 0);
  300. break;
  301. case LED_B2_ON:
  302. set_led_bit(hfc, driver_info->led_bits[3], 1);
  303. break;
  304. case LED_B2_OFF:
  305. set_led_bit(hfc, driver_info->led_bits[3], 0);
  306. break;
  307. }
  308. write_led(hfc, hfc->led_state);
  309. }
  310. /* ISDN l1 timer T3 expires */
  311. static void
  312. l1_timer_expire_t3(struct timer_list *t)
  313. {
  314. hfcusb_data *hfc = from_timer(hfc, t, t3_timer);
  315. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
  316. NULL);
  317. DBG(HFCUSB_DBG_STATES,
  318. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
  319. hfc->l1_activated = 0;
  320. handle_led(hfc, LED_S0_OFF);
  321. /* deactivate : */
  322. queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
  323. queue_control_request(hfc, HFCUSB_STATES, 3, 1);
  324. }
  325. /* ISDN l1 timer T4 expires */
  326. static void
  327. l1_timer_expire_t4(struct timer_list *t)
  328. {
  329. hfcusb_data *hfc = from_timer(hfc, t, t4_timer);
  330. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
  331. NULL);
  332. DBG(HFCUSB_DBG_STATES,
  333. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
  334. hfc->l1_activated = 0;
  335. handle_led(hfc, LED_S0_OFF);
  336. }
  337. /* S0 state changed */
  338. static void
  339. s0_state_handler(hfcusb_data *hfc, __u8 state)
  340. {
  341. __u8 old_state;
  342. old_state = hfc->l1_state;
  343. if (state == old_state || state < 1 || state > 8)
  344. return;
  345. DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
  346. old_state, state);
  347. if (state < 4 || state == 7 || state == 8) {
  348. if (timer_pending(&hfc->t3_timer))
  349. del_timer(&hfc->t3_timer);
  350. DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
  351. }
  352. if (state >= 7) {
  353. if (timer_pending(&hfc->t4_timer))
  354. del_timer(&hfc->t4_timer);
  355. DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
  356. }
  357. if (state == 7 && !hfc->l1_activated) {
  358. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
  359. PH_ACTIVATE | INDICATION, NULL);
  360. DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
  361. hfc->l1_activated = 1;
  362. handle_led(hfc, LED_S0_ON);
  363. } else if (state <= 3 /* && activated */) {
  364. if (old_state == 7 || old_state == 8) {
  365. DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
  366. if (!timer_pending(&hfc->t4_timer)) {
  367. hfc->t4_timer.expires =
  368. jiffies + (HFC_TIMER_T4 * HZ) / 1000;
  369. add_timer(&hfc->t4_timer);
  370. }
  371. } else {
  372. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
  373. PH_DEACTIVATE | INDICATION,
  374. NULL);
  375. DBG(HFCUSB_DBG_STATES,
  376. "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
  377. hfc->l1_activated = 0;
  378. handle_led(hfc, LED_S0_OFF);
  379. }
  380. }
  381. hfc->l1_state = state;
  382. }
  383. static void
  384. fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
  385. void *buf, int num_packets, int packet_size, int interval,
  386. usb_complete_t complete, void *context)
  387. {
  388. int k;
  389. usb_fill_int_urb(urb, dev, pipe, buf, packet_size * num_packets,
  390. complete, context, interval);
  391. urb->number_of_packets = num_packets;
  392. urb->transfer_flags = URB_ISO_ASAP;
  393. urb->actual_length = 0;
  394. for (k = 0; k < num_packets; k++) {
  395. urb->iso_frame_desc[k].offset = packet_size * k;
  396. urb->iso_frame_desc[k].length = packet_size;
  397. urb->iso_frame_desc[k].actual_length = 0;
  398. }
  399. }
  400. /* allocs urbs and start isoc transfer with two pending urbs to avoid
  401. * gaps in the transfer chain
  402. */
  403. static int
  404. start_isoc_chain(usb_fifo *fifo, int num_packets_per_urb,
  405. usb_complete_t complete, int packet_size)
  406. {
  407. int i, k, errcode;
  408. DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
  409. fifo->fifonum);
  410. /* allocate Memory for Iso out Urbs */
  411. for (i = 0; i < 2; i++) {
  412. if (!(fifo->iso[i].purb)) {
  413. fifo->iso[i].purb =
  414. usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
  415. if (!(fifo->iso[i].purb)) {
  416. printk(KERN_INFO
  417. "alloc urb for fifo %i failed!!!",
  418. fifo->fifonum);
  419. }
  420. fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
  421. /* Init the first iso */
  422. if (ISO_BUFFER_SIZE >=
  423. (fifo->usb_packet_maxlen *
  424. num_packets_per_urb)) {
  425. fill_isoc_urb(fifo->iso[i].purb,
  426. fifo->hfc->dev, fifo->pipe,
  427. fifo->iso[i].buffer,
  428. num_packets_per_urb,
  429. fifo->usb_packet_maxlen,
  430. fifo->intervall, complete,
  431. &fifo->iso[i]);
  432. memset(fifo->iso[i].buffer, 0,
  433. sizeof(fifo->iso[i].buffer));
  434. /* defining packet delimeters in fifo->buffer */
  435. for (k = 0; k < num_packets_per_urb; k++) {
  436. fifo->iso[i].purb->
  437. iso_frame_desc[k].offset =
  438. k * packet_size;
  439. fifo->iso[i].purb->
  440. iso_frame_desc[k].length =
  441. packet_size;
  442. }
  443. } else {
  444. printk(KERN_INFO
  445. "HFC-S USB: ISO Buffer size to small!\n");
  446. }
  447. }
  448. fifo->bit_line = BITLINE_INF;
  449. errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
  450. fifo->active = (errcode >= 0) ? 1 : 0;
  451. if (errcode < 0)
  452. printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
  453. i, errcode, symbolic(urb_errlist, errcode));
  454. }
  455. return (fifo->active);
  456. }
  457. /* stops running iso chain and frees their pending urbs */
  458. static void
  459. stop_isoc_chain(usb_fifo *fifo)
  460. {
  461. int i;
  462. for (i = 0; i < 2; i++) {
  463. if (fifo->iso[i].purb) {
  464. DBG(HFCUSB_DBG_INIT,
  465. "HFC-S USB: Stopping iso chain for fifo %i.%i",
  466. fifo->fifonum, i);
  467. usb_kill_urb(fifo->iso[i].purb);
  468. usb_free_urb(fifo->iso[i].purb);
  469. fifo->iso[i].purb = NULL;
  470. }
  471. }
  472. usb_kill_urb(fifo->urb);
  473. usb_free_urb(fifo->urb);
  474. fifo->urb = NULL;
  475. fifo->active = 0;
  476. }
  477. /* defines how much ISO packets are handled in one URB */
  478. static int iso_packets[8] =
  479. { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
  480. ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
  481. };
  482. static void
  483. tx_iso_complete(struct urb *urb)
  484. {
  485. iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
  486. usb_fifo *fifo = context_iso_urb->owner_fifo;
  487. hfcusb_data *hfc = fifo->hfc;
  488. int k, tx_offset, num_isoc_packets, sink, len, current_len,
  489. errcode;
  490. int frame_complete, transp_mode, fifon, status;
  491. __u8 threshbit;
  492. fifon = fifo->fifonum;
  493. status = urb->status;
  494. tx_offset = 0;
  495. /* ISO transfer only partially completed,
  496. look at individual frame status for details */
  497. if (status == -EXDEV) {
  498. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
  499. ", urb->status %d, fifonum %d\n",
  500. status, fifon);
  501. for (k = 0; k < iso_packets[fifon]; ++k) {
  502. errcode = urb->iso_frame_desc[k].status;
  503. if (errcode)
  504. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
  505. "packet %i, status: %i\n",
  506. k, errcode);
  507. }
  508. // clear status, so go on with ISO transfers
  509. status = 0;
  510. }
  511. if (fifo->active && !status) {
  512. transp_mode = 0;
  513. if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
  514. transp_mode = 1;
  515. /* is FifoFull-threshold set for our channel? */
  516. threshbit = (hfc->threshold_mask & (1 << fifon));
  517. num_isoc_packets = iso_packets[fifon];
  518. /* predict dataflow to avoid fifo overflow */
  519. if (fifon >= HFCUSB_D_TX) {
  520. sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
  521. } else {
  522. sink = (threshbit) ? SINK_MIN : SINK_MAX;
  523. }
  524. fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
  525. context_iso_urb->buffer, num_isoc_packets,
  526. fifo->usb_packet_maxlen, fifo->intervall,
  527. tx_iso_complete, urb->context);
  528. memset(context_iso_urb->buffer, 0,
  529. sizeof(context_iso_urb->buffer));
  530. frame_complete = 0;
  531. /* Generate next ISO Packets */
  532. for (k = 0; k < num_isoc_packets; ++k) {
  533. if (fifo->skbuff) {
  534. len = fifo->skbuff->len;
  535. /* we lower data margin every msec */
  536. fifo->bit_line -= sink;
  537. current_len = (0 - fifo->bit_line) / 8;
  538. /* maximum 15 byte for every ISO packet makes our life easier */
  539. if (current_len > 14)
  540. current_len = 14;
  541. current_len =
  542. (len <=
  543. current_len) ? len : current_len;
  544. /* how much bit do we put on the line? */
  545. fifo->bit_line += current_len * 8;
  546. context_iso_urb->buffer[tx_offset] = 0;
  547. if (current_len == len) {
  548. if (!transp_mode) {
  549. /* here frame completion */
  550. context_iso_urb->
  551. buffer[tx_offset] = 1;
  552. /* add 2 byte flags and 16bit CRC at end of ISDN frame */
  553. fifo->bit_line += 32;
  554. }
  555. frame_complete = 1;
  556. }
  557. memcpy(context_iso_urb->buffer +
  558. tx_offset + 1, fifo->skbuff->data,
  559. current_len);
  560. skb_pull(fifo->skbuff, current_len);
  561. /* define packet delimeters within the URB buffer */
  562. urb->iso_frame_desc[k].offset = tx_offset;
  563. urb->iso_frame_desc[k].length =
  564. current_len + 1;
  565. tx_offset += (current_len + 1);
  566. } else {
  567. urb->iso_frame_desc[k].offset =
  568. tx_offset++;
  569. urb->iso_frame_desc[k].length = 1;
  570. fifo->bit_line -= sink; /* we lower data margin every msec */
  571. if (fifo->bit_line < BITLINE_INF) {
  572. fifo->bit_line = BITLINE_INF;
  573. }
  574. }
  575. if (frame_complete) {
  576. fifo->delete_flg = 1;
  577. fifo->hif->l1l2(fifo->hif,
  578. PH_DATA | CONFIRM,
  579. (void *) (unsigned long) fifo->skbuff->
  580. truesize);
  581. if (fifo->skbuff && fifo->delete_flg) {
  582. dev_kfree_skb_any(fifo->skbuff);
  583. fifo->skbuff = NULL;
  584. fifo->delete_flg = 0;
  585. }
  586. frame_complete = 0;
  587. }
  588. }
  589. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  590. if (errcode < 0) {
  591. printk(KERN_INFO
  592. "HFC-S USB: error submitting ISO URB: %d\n",
  593. errcode);
  594. }
  595. } else {
  596. if (status && !hfc->disc_flag) {
  597. printk(KERN_INFO
  598. "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
  599. status, symbolic(urb_errlist, status), fifon);
  600. }
  601. }
  602. }
  603. static void
  604. rx_iso_complete(struct urb *urb)
  605. {
  606. iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
  607. usb_fifo *fifo = context_iso_urb->owner_fifo;
  608. hfcusb_data *hfc = fifo->hfc;
  609. int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
  610. status;
  611. unsigned int iso_status;
  612. __u8 *buf;
  613. static __u8 eof[8];
  614. fifon = fifo->fifonum;
  615. status = urb->status;
  616. if (urb->status == -EOVERFLOW) {
  617. DBG(HFCUSB_DBG_VERBOSE_USB,
  618. "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
  619. status = 0;
  620. }
  621. /* ISO transfer only partially completed,
  622. look at individual frame status for details */
  623. if (status == -EXDEV) {
  624. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
  625. "urb->status %d, fifonum %d\n",
  626. status, fifon);
  627. status = 0;
  628. }
  629. if (fifo->active && !status) {
  630. num_isoc_packets = iso_packets[fifon];
  631. maxlen = fifo->usb_packet_maxlen;
  632. for (k = 0; k < num_isoc_packets; ++k) {
  633. len = urb->iso_frame_desc[k].actual_length;
  634. offset = urb->iso_frame_desc[k].offset;
  635. buf = context_iso_urb->buffer + offset;
  636. iso_status = urb->iso_frame_desc[k].status;
  637. if (iso_status && !hfc->disc_flag)
  638. DBG(HFCUSB_DBG_VERBOSE_USB,
  639. "HFC-S USB: rx_iso_complete "
  640. "ISO packet %i, status: %i\n",
  641. k, iso_status);
  642. if (fifon == HFCUSB_D_RX) {
  643. DBG(HFCUSB_DBG_VERBOSE_USB,
  644. "HFC-S USB: ISO-D-RX lst_urblen:%2d "
  645. "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
  646. fifo->last_urblen, len, maxlen,
  647. eof[5]);
  648. DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
  649. }
  650. if (fifo->last_urblen != maxlen) {
  651. /* the threshold mask is in the 2nd status byte */
  652. hfc->threshold_mask = buf[1];
  653. /* care for L1 state only for D-Channel
  654. to avoid overlapped iso completions */
  655. if (fifon == HFCUSB_D_RX) {
  656. /* the S0 state is in the upper half
  657. of the 1st status byte */
  658. s0_state_handler(hfc, buf[0] >> 4);
  659. }
  660. eof[fifon] = buf[0] & 1;
  661. if (len > 2)
  662. collect_rx_frame(fifo, buf + 2,
  663. len - 2,
  664. (len < maxlen) ?
  665. eof[fifon] : 0);
  666. } else {
  667. collect_rx_frame(fifo, buf, len,
  668. (len <
  669. maxlen) ? eof[fifon] :
  670. 0);
  671. }
  672. fifo->last_urblen = len;
  673. }
  674. fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
  675. context_iso_urb->buffer, num_isoc_packets,
  676. fifo->usb_packet_maxlen, fifo->intervall,
  677. rx_iso_complete, urb->context);
  678. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  679. if (errcode < 0) {
  680. printk(KERN_ERR
  681. "HFC-S USB: error submitting ISO URB: %d\n",
  682. errcode);
  683. }
  684. } else {
  685. if (status && !hfc->disc_flag) {
  686. printk(KERN_ERR
  687. "HFC-S USB: rx_iso_complete : "
  688. "urb->status %d, fifonum %d\n",
  689. status, fifon);
  690. }
  691. }
  692. }
  693. /* collect rx data from INT- and ISO-URBs */
  694. static void
  695. collect_rx_frame(usb_fifo *fifo, __u8 *data, int len, int finish)
  696. {
  697. hfcusb_data *hfc = fifo->hfc;
  698. int transp_mode, fifon;
  699. fifon = fifo->fifonum;
  700. transp_mode = 0;
  701. if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
  702. transp_mode = 1;
  703. if (!fifo->skbuff) {
  704. fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
  705. if (!fifo->skbuff) {
  706. printk(KERN_ERR
  707. "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
  708. fifon);
  709. return;
  710. }
  711. }
  712. if (len) {
  713. if (fifo->skbuff->len + len < fifo->max_size) {
  714. skb_put_data(fifo->skbuff, data, len);
  715. } else {
  716. DBG(HFCUSB_DBG_FIFO_ERR,
  717. "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
  718. fifo->max_size, fifon);
  719. DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
  720. skb_trim(fifo->skbuff, 0);
  721. }
  722. }
  723. if (transp_mode && fifo->skbuff->len >= 128) {
  724. fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
  725. fifo->skbuff);
  726. fifo->skbuff = NULL;
  727. return;
  728. }
  729. /* we have a complete hdlc packet */
  730. if (finish) {
  731. if (fifo->skbuff->len > 3 &&
  732. !fifo->skbuff->data[fifo->skbuff->len - 1]) {
  733. if (fifon == HFCUSB_D_RX) {
  734. DBG(HFCUSB_DBG_DCHANNEL,
  735. "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
  736. DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
  737. }
  738. /* remove CRC & status */
  739. skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
  740. if (fifon == HFCUSB_PCM_RX) {
  741. fifo->hif->l1l2(fifo->hif,
  742. PH_DATA_E | INDICATION,
  743. fifo->skbuff);
  744. } else
  745. fifo->hif->l1l2(fifo->hif,
  746. PH_DATA | INDICATION,
  747. fifo->skbuff);
  748. fifo->skbuff = NULL; /* buffer was freed from upper layer */
  749. } else {
  750. DBG(HFCUSB_DBG_FIFO_ERR,
  751. "HFC-S USB: ERROR frame len(%d) fifo(%d)",
  752. fifo->skbuff->len, fifon);
  753. DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
  754. skb_trim(fifo->skbuff, 0);
  755. }
  756. }
  757. }
  758. static void
  759. rx_int_complete(struct urb *urb)
  760. {
  761. int len;
  762. int status;
  763. __u8 *buf, maxlen, fifon;
  764. usb_fifo *fifo = (usb_fifo *) urb->context;
  765. hfcusb_data *hfc = fifo->hfc;
  766. static __u8 eof[8];
  767. urb->dev = hfc->dev; /* security init */
  768. fifon = fifo->fifonum;
  769. if ((!fifo->active) || (urb->status)) {
  770. DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
  771. fifon, urb->status);
  772. fifo->urb->interval = 0; /* cancel automatic rescheduling */
  773. if (fifo->skbuff) {
  774. dev_kfree_skb_any(fifo->skbuff);
  775. fifo->skbuff = NULL;
  776. }
  777. return;
  778. }
  779. len = urb->actual_length;
  780. buf = fifo->buffer;
  781. maxlen = fifo->usb_packet_maxlen;
  782. if (fifon == HFCUSB_D_RX) {
  783. DBG(HFCUSB_DBG_VERBOSE_USB,
  784. "HFC-S USB: INT-D-RX lst_urblen:%2d "
  785. "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
  786. fifo->last_urblen, len, maxlen,
  787. eof[5]);
  788. DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
  789. }
  790. if (fifo->last_urblen != fifo->usb_packet_maxlen) {
  791. /* the threshold mask is in the 2nd status byte */
  792. hfc->threshold_mask = buf[1];
  793. /* the S0 state is in the upper half of the 1st status byte */
  794. s0_state_handler(hfc, buf[0] >> 4);
  795. eof[fifon] = buf[0] & 1;
  796. /* if we have more than the 2 status bytes -> collect data */
  797. if (len > 2)
  798. collect_rx_frame(fifo, buf + 2,
  799. urb->actual_length - 2,
  800. (len < maxlen) ? eof[fifon] : 0);
  801. } else {
  802. collect_rx_frame(fifo, buf, urb->actual_length,
  803. (len < maxlen) ? eof[fifon] : 0);
  804. }
  805. fifo->last_urblen = urb->actual_length;
  806. status = usb_submit_urb(urb, GFP_ATOMIC);
  807. if (status) {
  808. printk(KERN_INFO
  809. "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
  810. __func__, fifon);
  811. }
  812. }
  813. /* start initial INT-URB for certain fifo */
  814. static void
  815. start_int_fifo(usb_fifo *fifo)
  816. {
  817. int errcode;
  818. DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
  819. fifo->fifonum);
  820. if (!fifo->urb) {
  821. fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
  822. if (!fifo->urb)
  823. return;
  824. }
  825. usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
  826. fifo->buffer, fifo->usb_packet_maxlen,
  827. rx_int_complete, fifo, fifo->intervall);
  828. fifo->active = 1; /* must be marked active */
  829. errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
  830. if (errcode) {
  831. printk(KERN_ERR "HFC-S USB: submit URB error(%s): status:%i\n",
  832. __func__, errcode);
  833. fifo->active = 0;
  834. fifo->skbuff = NULL;
  835. }
  836. }
  837. static void
  838. setup_bchannel(hfcusb_data *hfc, int channel, int mode)
  839. {
  840. __u8 val, idx_table[2] = { 0, 2 };
  841. if (hfc->disc_flag) {
  842. return;
  843. }
  844. DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
  845. channel, mode);
  846. hfc->b_mode[channel] = mode;
  847. /* setup CON_HDLC */
  848. val = 0;
  849. if (mode != L1_MODE_NULL)
  850. val = 8; /* enable fifo? */
  851. if (mode == L1_MODE_TRANS)
  852. val |= 2; /* set transparent bit */
  853. /* set FIFO to transmit register */
  854. queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 1);
  855. queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
  856. /* reset fifo */
  857. queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
  858. /* set FIFO to receive register */
  859. queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
  860. queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
  861. /* reset fifo */
  862. queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
  863. val = 0x40;
  864. if (hfc->b_mode[0])
  865. val |= 1;
  866. if (hfc->b_mode[1])
  867. val |= 2;
  868. queue_control_request(hfc, HFCUSB_SCTRL, val, 1);
  869. val = 0;
  870. if (hfc->b_mode[0])
  871. val |= 1;
  872. if (hfc->b_mode[1])
  873. val |= 2;
  874. queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);
  875. if (mode == L1_MODE_NULL) {
  876. if (channel)
  877. handle_led(hfc, LED_B2_OFF);
  878. else
  879. handle_led(hfc, LED_B1_OFF);
  880. } else {
  881. if (channel)
  882. handle_led(hfc, LED_B2_ON);
  883. else
  884. handle_led(hfc, LED_B1_ON);
  885. }
  886. }
  887. static void
  888. hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
  889. {
  890. usb_fifo *fifo = my_hisax_if->priv;
  891. hfcusb_data *hfc = fifo->hfc;
  892. switch (pr) {
  893. case PH_ACTIVATE | REQUEST:
  894. if (fifo->fifonum == HFCUSB_D_TX) {
  895. DBG(HFCUSB_DBG_STATES,
  896. "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
  897. if (hfc->l1_state != 3
  898. && hfc->l1_state != 7) {
  899. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
  900. PH_DEACTIVATE |
  901. INDICATION,
  902. NULL);
  903. DBG(HFCUSB_DBG_STATES,
  904. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
  905. } else {
  906. if (hfc->l1_state == 7) { /* l1 already active */
  907. hfc->d_if.ifc.l1l2(&hfc->
  908. d_if.
  909. ifc,
  910. PH_ACTIVATE
  911. |
  912. INDICATION,
  913. NULL);
  914. DBG(HFCUSB_DBG_STATES,
  915. "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
  916. } else {
  917. /* force sending sending INFO1 */
  918. queue_control_request(hfc,
  919. HFCUSB_STATES,
  920. 0x14,
  921. 1);
  922. mdelay(1);
  923. /* start l1 activation */
  924. queue_control_request(hfc,
  925. HFCUSB_STATES,
  926. 0x04,
  927. 1);
  928. if (!timer_pending
  929. (&hfc->t3_timer)) {
  930. hfc->t3_timer.
  931. expires =
  932. jiffies +
  933. (HFC_TIMER_T3 *
  934. HZ) / 1000;
  935. add_timer(&hfc->
  936. t3_timer);
  937. }
  938. }
  939. }
  940. } else {
  941. DBG(HFCUSB_DBG_STATES,
  942. "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
  943. setup_bchannel(hfc,
  944. (fifo->fifonum ==
  945. HFCUSB_B1_TX) ? 0 : 1,
  946. (long) arg);
  947. fifo->hif->l1l2(fifo->hif,
  948. PH_ACTIVATE | INDICATION,
  949. NULL);
  950. }
  951. break;
  952. case PH_DEACTIVATE | REQUEST:
  953. if (fifo->fifonum == HFCUSB_D_TX) {
  954. DBG(HFCUSB_DBG_STATES,
  955. "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
  956. } else {
  957. DBG(HFCUSB_DBG_STATES,
  958. "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
  959. setup_bchannel(hfc,
  960. (fifo->fifonum ==
  961. HFCUSB_B1_TX) ? 0 : 1,
  962. (int) L1_MODE_NULL);
  963. fifo->hif->l1l2(fifo->hif,
  964. PH_DEACTIVATE | INDICATION,
  965. NULL);
  966. }
  967. break;
  968. case PH_DATA | REQUEST:
  969. if (fifo->skbuff && fifo->delete_flg) {
  970. dev_kfree_skb_any(fifo->skbuff);
  971. fifo->skbuff = NULL;
  972. fifo->delete_flg = 0;
  973. }
  974. fifo->skbuff = arg; /* we have a new buffer */
  975. break;
  976. default:
  977. DBG(HFCUSB_DBG_STATES,
  978. "HFC_USB: hfc_usb_d_l2l1: unknown state : %#x", pr);
  979. break;
  980. }
  981. }
  982. /* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
  983. static int
  984. hfc_usb_init(hfcusb_data *hfc)
  985. {
  986. usb_fifo *fifo;
  987. int i;
  988. u_char b;
  989. struct hisax_b_if *p_b_if[2];
  990. /* check the chip id */
  991. if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
  992. printk(KERN_INFO "HFC-USB: cannot read chip id\n");
  993. return (1);
  994. }
  995. if (b != HFCUSB_CHIPID) {
  996. printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
  997. return (1);
  998. }
  999. /* first set the needed config, interface and alternate */
  1000. usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
  1001. /* do Chip reset */
  1002. write_usb(hfc, HFCUSB_CIRM, 8);
  1003. /* aux = output, reset off */
  1004. write_usb(hfc, HFCUSB_CIRM, 0x10);
  1005. /* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
  1006. write_usb(hfc, HFCUSB_USB_SIZE,
  1007. (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));
  1008. /* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
  1009. write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
  1010. /* enable PCM/GCI master mode */
  1011. write_usb(hfc, HFCUSB_MST_MODE1, 0); /* set default values */
  1012. write_usb(hfc, HFCUSB_MST_MODE0, 1); /* enable master mode */
  1013. /* init the fifos */
  1014. write_usb(hfc, HFCUSB_F_THRES,
  1015. (HFCUSB_TX_THRESHOLD /
  1016. 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
  1017. fifo = hfc->fifos;
  1018. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1019. write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
  1020. fifo[i].skbuff = NULL; /* init buffer pointer */
  1021. fifo[i].max_size =
  1022. (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
  1023. fifo[i].last_urblen = 0;
  1024. /* set 2 bit for D- & E-channel */
  1025. write_usb(hfc, HFCUSB_HDLC_PAR,
  1026. ((i <= HFCUSB_B2_RX) ? 0 : 2));
  1027. /* rx hdlc, enable IFF for D-channel */
  1028. write_usb(hfc, HFCUSB_CON_HDLC,
  1029. ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
  1030. write_usb(hfc, HFCUSB_INC_RES_F, 2); /* reset the fifo */
  1031. }
  1032. write_usb(hfc, HFCUSB_CLKDEL, 0x0f); /* clock delay value */
  1033. write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
  1034. write_usb(hfc, HFCUSB_STATES, 3); /* enable state machine */
  1035. write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
  1036. write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
  1037. /* set both B-channel to not connected */
  1038. hfc->b_mode[0] = L1_MODE_NULL;
  1039. hfc->b_mode[1] = L1_MODE_NULL;
  1040. hfc->l1_activated = 0;
  1041. hfc->disc_flag = 0;
  1042. hfc->led_state = 0;
  1043. hfc->old_led_state = 0;
  1044. /* init the t3 timer */
  1045. timer_setup(&hfc->t3_timer, l1_timer_expire_t3, 0);
  1046. /* init the t4 timer */
  1047. timer_setup(&hfc->t4_timer, l1_timer_expire_t4, 0);
  1048. /* init the background machinery for control requests */
  1049. hfc->ctrl_read.bRequestType = 0xc0;
  1050. hfc->ctrl_read.bRequest = 1;
  1051. hfc->ctrl_read.wLength = cpu_to_le16(1);
  1052. hfc->ctrl_write.bRequestType = 0x40;
  1053. hfc->ctrl_write.bRequest = 0;
  1054. hfc->ctrl_write.wLength = 0;
  1055. usb_fill_control_urb(hfc->ctrl_urb,
  1056. hfc->dev,
  1057. hfc->ctrl_out_pipe,
  1058. (u_char *)&hfc->ctrl_write,
  1059. NULL, 0, ctrl_complete, hfc);
  1060. /* Init All Fifos */
  1061. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1062. hfc->fifos[i].iso[0].purb = NULL;
  1063. hfc->fifos[i].iso[1].purb = NULL;
  1064. hfc->fifos[i].active = 0;
  1065. }
  1066. /* register Modul to upper Hisax Layers */
  1067. hfc->d_if.owner = THIS_MODULE;
  1068. hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
  1069. hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
  1070. for (i = 0; i < 2; i++) {
  1071. hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
  1072. hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
  1073. p_b_if[i] = &hfc->b_if[i];
  1074. }
  1075. /* default Prot: EURO ISDN, should be a module_param */
  1076. hfc->protocol = 2;
  1077. i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
  1078. if (i) {
  1079. printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
  1080. return i;
  1081. }
  1082. #ifdef CONFIG_HISAX_DEBUG
  1083. hfc_debug = debug;
  1084. #endif
  1085. for (i = 0; i < 4; i++)
  1086. hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
  1087. for (i = 4; i < 8; i++)
  1088. hfc->fifos[i].hif = &hfc->d_if.ifc;
  1089. /* 3 (+1) INT IN + 3 ISO OUT */
  1090. if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
  1091. start_int_fifo(hfc->fifos + HFCUSB_D_RX);
  1092. if (hfc->fifos[HFCUSB_PCM_RX].pipe)
  1093. start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
  1094. start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
  1095. start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
  1096. }
  1097. /* 3 (+1) ISO IN + 3 ISO OUT */
  1098. if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
  1099. start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
  1100. rx_iso_complete, 16);
  1101. if (hfc->fifos[HFCUSB_PCM_RX].pipe)
  1102. start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
  1103. ISOC_PACKETS_D, rx_iso_complete,
  1104. 16);
  1105. start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
  1106. rx_iso_complete, 16);
  1107. start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
  1108. rx_iso_complete, 16);
  1109. }
  1110. start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
  1111. tx_iso_complete, 1);
  1112. start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
  1113. tx_iso_complete, 1);
  1114. start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
  1115. tx_iso_complete, 1);
  1116. handle_led(hfc, LED_POWER_ON);
  1117. return (0);
  1118. }
  1119. /* initial callback for each plugged USB device */
  1120. static int
  1121. hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1122. {
  1123. struct usb_device *dev = interface_to_usbdev(intf);
  1124. hfcusb_data *context;
  1125. struct usb_host_interface *iface = intf->cur_altsetting;
  1126. struct usb_host_interface *iface_used = NULL;
  1127. struct usb_host_endpoint *ep;
  1128. int ifnum = iface->desc.bInterfaceNumber;
  1129. int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
  1130. attr, cfg_found, cidx, ep_addr;
  1131. int cmptbl[16], small_match, iso_packet_size, packet_size,
  1132. alt_used = 0;
  1133. hfcsusb_vdata *driver_info;
  1134. vend_idx = 0xffff;
  1135. for (i = 0; hfcusb_idtab[i].idVendor; i++) {
  1136. if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
  1137. && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
  1138. vend_idx = i;
  1139. continue;
  1140. }
  1141. }
  1142. printk(KERN_INFO
  1143. "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
  1144. ifnum, iface->desc.bAlternateSetting, intf->minor);
  1145. if (vend_idx != 0xffff) {
  1146. /* if vendor and product ID is OK, start probing alternate settings */
  1147. alt_idx = 0;
  1148. small_match = 0xffff;
  1149. /* default settings */
  1150. iso_packet_size = 16;
  1151. packet_size = 64;
  1152. while (alt_idx < intf->num_altsetting) {
  1153. iface = intf->altsetting + alt_idx;
  1154. probe_alt_setting = iface->desc.bAlternateSetting;
  1155. cfg_used = 0;
  1156. /* check for config EOL element */
  1157. while (validconf[cfg_used][0]) {
  1158. cfg_found = 1;
  1159. vcf = validconf[cfg_used];
  1160. /* first endpoint descriptor */
  1161. ep = iface->endpoint;
  1162. memcpy(cmptbl, vcf, 16 * sizeof(int));
  1163. /* check for all endpoints in this alternate setting */
  1164. for (i = 0; i < iface->desc.bNumEndpoints;
  1165. i++) {
  1166. ep_addr =
  1167. ep->desc.bEndpointAddress;
  1168. /* get endpoint base */
  1169. idx = ((ep_addr & 0x7f) - 1) * 2;
  1170. if (ep_addr & 0x80)
  1171. idx++;
  1172. attr = ep->desc.bmAttributes;
  1173. if (cmptbl[idx] == EP_NUL) {
  1174. cfg_found = 0;
  1175. }
  1176. if (attr == USB_ENDPOINT_XFER_INT
  1177. && cmptbl[idx] == EP_INT)
  1178. cmptbl[idx] = EP_NUL;
  1179. if (attr == USB_ENDPOINT_XFER_BULK
  1180. && cmptbl[idx] == EP_BLK)
  1181. cmptbl[idx] = EP_NUL;
  1182. if (attr == USB_ENDPOINT_XFER_ISOC
  1183. && cmptbl[idx] == EP_ISO)
  1184. cmptbl[idx] = EP_NUL;
  1185. /* check if all INT endpoints match minimum interval */
  1186. if ((attr == USB_ENDPOINT_XFER_INT)
  1187. && (ep->desc.bInterval < vcf[17])) {
  1188. cfg_found = 0;
  1189. }
  1190. ep++;
  1191. }
  1192. for (i = 0; i < 16; i++) {
  1193. /* all entries must be EP_NOP or EP_NUL for a valid config */
  1194. if (cmptbl[i] != EP_NOP
  1195. && cmptbl[i] != EP_NUL)
  1196. cfg_found = 0;
  1197. }
  1198. if (cfg_found) {
  1199. if (cfg_used < small_match) {
  1200. small_match = cfg_used;
  1201. alt_used =
  1202. probe_alt_setting;
  1203. iface_used = iface;
  1204. }
  1205. }
  1206. cfg_used++;
  1207. }
  1208. alt_idx++;
  1209. } /* (alt_idx < intf->num_altsetting) */
  1210. /* found a valid USB Ta Endpint config */
  1211. if (small_match != 0xffff) {
  1212. iface = iface_used;
  1213. if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
  1214. return (-ENOMEM); /* got no mem */
  1215. ep = iface->endpoint;
  1216. vcf = validconf[small_match];
  1217. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1218. ep_addr = ep->desc.bEndpointAddress;
  1219. /* get endpoint base */
  1220. idx = ((ep_addr & 0x7f) - 1) * 2;
  1221. if (ep_addr & 0x80)
  1222. idx++;
  1223. cidx = idx & 7;
  1224. attr = ep->desc.bmAttributes;
  1225. /* init Endpoints */
  1226. if (vcf[idx] != EP_NOP
  1227. && vcf[idx] != EP_NUL) {
  1228. switch (attr) {
  1229. case USB_ENDPOINT_XFER_INT:
  1230. context->
  1231. fifos[cidx].
  1232. pipe =
  1233. usb_rcvintpipe
  1234. (dev,
  1235. ep->desc.
  1236. bEndpointAddress);
  1237. context->
  1238. fifos[cidx].
  1239. usb_transfer_mode
  1240. = USB_INT;
  1241. packet_size =
  1242. le16_to_cpu(ep->desc.wMaxPacketSize);
  1243. break;
  1244. case USB_ENDPOINT_XFER_BULK:
  1245. if (ep_addr & 0x80)
  1246. context->
  1247. fifos
  1248. [cidx].
  1249. pipe =
  1250. usb_rcvbulkpipe
  1251. (dev,
  1252. ep->
  1253. desc.
  1254. bEndpointAddress);
  1255. else
  1256. context->
  1257. fifos
  1258. [cidx].
  1259. pipe =
  1260. usb_sndbulkpipe
  1261. (dev,
  1262. ep->
  1263. desc.
  1264. bEndpointAddress);
  1265. context->
  1266. fifos[cidx].
  1267. usb_transfer_mode
  1268. = USB_BULK;
  1269. packet_size =
  1270. le16_to_cpu(ep->desc.wMaxPacketSize);
  1271. break;
  1272. case USB_ENDPOINT_XFER_ISOC:
  1273. if (ep_addr & 0x80)
  1274. context->
  1275. fifos
  1276. [cidx].
  1277. pipe =
  1278. usb_rcvisocpipe
  1279. (dev,
  1280. ep->
  1281. desc.
  1282. bEndpointAddress);
  1283. else
  1284. context->
  1285. fifos
  1286. [cidx].
  1287. pipe =
  1288. usb_sndisocpipe
  1289. (dev,
  1290. ep->
  1291. desc.
  1292. bEndpointAddress);
  1293. context->
  1294. fifos[cidx].
  1295. usb_transfer_mode
  1296. = USB_ISOC;
  1297. iso_packet_size =
  1298. le16_to_cpu(ep->desc.wMaxPacketSize);
  1299. break;
  1300. default:
  1301. context->
  1302. fifos[cidx].
  1303. pipe = 0;
  1304. } /* switch attribute */
  1305. if (context->fifos[cidx].pipe) {
  1306. context->fifos[cidx].
  1307. fifonum = cidx;
  1308. context->fifos[cidx].hfc =
  1309. context;
  1310. context->fifos[cidx].usb_packet_maxlen =
  1311. le16_to_cpu(ep->desc.wMaxPacketSize);
  1312. context->fifos[cidx].
  1313. intervall =
  1314. ep->desc.bInterval;
  1315. context->fifos[cidx].
  1316. skbuff = NULL;
  1317. }
  1318. }
  1319. ep++;
  1320. }
  1321. context->dev = dev; /* save device */
  1322. context->if_used = ifnum; /* save used interface */
  1323. context->alt_used = alt_used; /* and alternate config */
  1324. context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
  1325. context->cfg_used = vcf[16]; /* store used config */
  1326. context->vend_idx = vend_idx; /* store found vendor */
  1327. context->packet_size = packet_size;
  1328. context->iso_packet_size = iso_packet_size;
  1329. /* create the control pipes needed for register access */
  1330. context->ctrl_in_pipe =
  1331. usb_rcvctrlpipe(context->dev, 0);
  1332. context->ctrl_out_pipe =
  1333. usb_sndctrlpipe(context->dev, 0);
  1334. driver_info = (hfcsusb_vdata *)
  1335. hfcusb_idtab[vend_idx].driver_info;
  1336. context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  1337. if (!context->ctrl_urb) {
  1338. pr_warn("%s: No memory for control urb\n",
  1339. driver_info->vend_name);
  1340. kfree(context);
  1341. return -ENOMEM;
  1342. }
  1343. pr_info("HFC-S USB: detected \"%s\"\n",
  1344. driver_info->vend_name);
  1345. DBG(HFCUSB_DBG_INIT,
  1346. "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
  1347. conf_str[small_match], context->if_used,
  1348. context->alt_used,
  1349. validconf[small_match][18]);
  1350. /* init the chip and register the driver */
  1351. if (hfc_usb_init(context)) {
  1352. usb_kill_urb(context->ctrl_urb);
  1353. usb_free_urb(context->ctrl_urb);
  1354. context->ctrl_urb = NULL;
  1355. kfree(context);
  1356. return (-EIO);
  1357. }
  1358. usb_set_intfdata(intf, context);
  1359. return (0);
  1360. }
  1361. } else {
  1362. printk(KERN_INFO
  1363. "HFC-S USB: no valid vendor found in USB descriptor\n");
  1364. }
  1365. return (-EIO);
  1366. }
  1367. /* callback for unplugged USB device */
  1368. static void
  1369. hfc_usb_disconnect(struct usb_interface *intf)
  1370. {
  1371. hfcusb_data *context = usb_get_intfdata(intf);
  1372. int i;
  1373. handle_led(context, LED_POWER_OFF);
  1374. schedule_timeout(HZ / 100);
  1375. printk(KERN_INFO "HFC-S USB: device disconnect\n");
  1376. context->disc_flag = 1;
  1377. usb_set_intfdata(intf, NULL);
  1378. if (timer_pending(&context->t3_timer))
  1379. del_timer(&context->t3_timer);
  1380. if (timer_pending(&context->t4_timer))
  1381. del_timer(&context->t4_timer);
  1382. /* tell all fifos to terminate */
  1383. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1384. if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
  1385. if (context->fifos[i].active > 0) {
  1386. stop_isoc_chain(&context->fifos[i]);
  1387. DBG(HFCUSB_DBG_INIT,
  1388. "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
  1389. __func__, i);
  1390. }
  1391. } else {
  1392. if (context->fifos[i].active > 0) {
  1393. context->fifos[i].active = 0;
  1394. DBG(HFCUSB_DBG_INIT,
  1395. "HFC-S USB: %s unlinking URB for Fifo(%i)",
  1396. __func__, i);
  1397. }
  1398. usb_kill_urb(context->fifos[i].urb);
  1399. usb_free_urb(context->fifos[i].urb);
  1400. context->fifos[i].urb = NULL;
  1401. }
  1402. context->fifos[i].active = 0;
  1403. }
  1404. usb_kill_urb(context->ctrl_urb);
  1405. usb_free_urb(context->ctrl_urb);
  1406. context->ctrl_urb = NULL;
  1407. hisax_unregister(&context->d_if);
  1408. kfree(context); /* free our structure again */
  1409. }
  1410. static struct usb_driver hfc_drv = {
  1411. .name = "hfc_usb",
  1412. .id_table = hfcusb_idtab,
  1413. .probe = hfc_usb_probe,
  1414. .disconnect = hfc_usb_disconnect,
  1415. .disable_hub_initiated_lpm = 1,
  1416. };
  1417. static void __exit
  1418. hfc_usb_mod_exit(void)
  1419. {
  1420. usb_deregister(&hfc_drv); /* release our driver */
  1421. printk(KERN_INFO "HFC-S USB: module removed\n");
  1422. }
  1423. static int __init
  1424. hfc_usb_mod_init(void)
  1425. {
  1426. char revstr[30], datestr[30], dummy[30];
  1427. #ifndef CONFIG_HISAX_DEBUG
  1428. hfc_debug = debug;
  1429. #endif
  1430. sscanf(hfcusb_revision,
  1431. "%s %s $ %s %s %s $ ", dummy, revstr,
  1432. dummy, datestr, dummy);
  1433. printk(KERN_INFO
  1434. "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
  1435. revstr, datestr, debug);
  1436. if (usb_register(&hfc_drv)) {
  1437. printk(KERN_INFO
  1438. "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
  1439. return (-1); /* unable to register */
  1440. }
  1441. return (0);
  1442. }
  1443. module_init(hfc_usb_mod_init);
  1444. module_exit(hfc_usb_mod_exit);
  1445. MODULE_AUTHOR(DRIVER_AUTHOR);
  1446. MODULE_DESCRIPTION(DRIVER_DESC);
  1447. MODULE_LICENSE("GPL");
  1448. MODULE_DEVICE_TABLE(usb, hfcusb_idtab);