hfc_usb.c 44 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/smp_lock.h>
  40. #include <linux/sched.h>
  41. #include <linux/moduleparam.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 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. ctrl_buft *buf;
  236. urb->dev = hfc->dev;
  237. if (hfc->ctrl_cnt) {
  238. buf = &hfc->ctrl_buff[hfc->ctrl_out_idx];
  239. hfc->ctrl_cnt--; /* decrement actual count */
  240. if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
  241. hfc->ctrl_out_idx = 0; /* pointer wrap */
  242. ctrl_start_transfer(hfc); /* start next transfer */
  243. }
  244. }
  245. /* write led data to auxport & invert if necessary */
  246. static void
  247. write_led(hfcusb_data * hfc, __u8 led_state)
  248. {
  249. if (led_state != hfc->old_led_state) {
  250. hfc->old_led_state = led_state;
  251. queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
  252. }
  253. }
  254. static void
  255. set_led_bit(hfcusb_data * hfc, signed short led_bits, int on)
  256. {
  257. if (on) {
  258. if (led_bits < 0)
  259. hfc->led_state &= ~abs(led_bits);
  260. else
  261. hfc->led_state |= led_bits;
  262. } else {
  263. if (led_bits < 0)
  264. hfc->led_state |= abs(led_bits);
  265. else
  266. hfc->led_state &= ~led_bits;
  267. }
  268. }
  269. /* handle LED requests */
  270. static void
  271. handle_led(hfcusb_data * hfc, int event)
  272. {
  273. hfcsusb_vdata *driver_info =
  274. (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
  275. /* if no scheme -> no LED action */
  276. if (driver_info->led_scheme == LED_OFF)
  277. return;
  278. switch (event) {
  279. case LED_POWER_ON:
  280. set_led_bit(hfc, driver_info->led_bits[0], 1);
  281. set_led_bit(hfc, driver_info->led_bits[1], 0);
  282. set_led_bit(hfc, driver_info->led_bits[2], 0);
  283. set_led_bit(hfc, driver_info->led_bits[3], 0);
  284. break;
  285. case LED_POWER_OFF:
  286. set_led_bit(hfc, driver_info->led_bits[0], 0);
  287. set_led_bit(hfc, driver_info->led_bits[1], 0);
  288. set_led_bit(hfc, driver_info->led_bits[2], 0);
  289. set_led_bit(hfc, driver_info->led_bits[3], 0);
  290. break;
  291. case LED_S0_ON:
  292. set_led_bit(hfc, driver_info->led_bits[1], 1);
  293. break;
  294. case LED_S0_OFF:
  295. set_led_bit(hfc, driver_info->led_bits[1], 0);
  296. break;
  297. case LED_B1_ON:
  298. set_led_bit(hfc, driver_info->led_bits[2], 1);
  299. break;
  300. case LED_B1_OFF:
  301. set_led_bit(hfc, driver_info->led_bits[2], 0);
  302. break;
  303. case LED_B2_ON:
  304. set_led_bit(hfc, driver_info->led_bits[3], 1);
  305. break;
  306. case LED_B2_OFF:
  307. set_led_bit(hfc, driver_info->led_bits[3], 0);
  308. break;
  309. }
  310. write_led(hfc, hfc->led_state);
  311. }
  312. /* ISDN l1 timer T3 expires */
  313. static void
  314. l1_timer_expire_t3(hfcusb_data * hfc)
  315. {
  316. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
  317. NULL);
  318. DBG(HFCUSB_DBG_STATES,
  319. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
  320. hfc->l1_activated = 0;
  321. handle_led(hfc, LED_S0_OFF);
  322. /* deactivate : */
  323. queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
  324. queue_control_request(hfc, HFCUSB_STATES, 3, 1);
  325. }
  326. /* ISDN l1 timer T4 expires */
  327. static void
  328. l1_timer_expire_t4(hfcusb_data * hfc)
  329. {
  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. urb->dev = dev;
  390. urb->pipe = pipe;
  391. urb->complete = complete;
  392. urb->number_of_packets = num_packets;
  393. urb->transfer_buffer_length = packet_size * num_packets;
  394. urb->context = context;
  395. urb->transfer_buffer = buf;
  396. urb->transfer_flags = URB_ISO_ASAP;
  397. urb->actual_length = 0;
  398. urb->interval = interval;
  399. for (k = 0; k < num_packets; k++) {
  400. urb->iso_frame_desc[k].offset = packet_size * k;
  401. urb->iso_frame_desc[k].length = packet_size;
  402. urb->iso_frame_desc[k].actual_length = 0;
  403. }
  404. }
  405. /* allocs urbs and start isoc transfer with two pending urbs to avoid
  406. * gaps in the transfer chain
  407. */
  408. static int
  409. start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,
  410. usb_complete_t complete, int packet_size)
  411. {
  412. int i, k, errcode;
  413. DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
  414. fifo->fifonum);
  415. /* allocate Memory for Iso out Urbs */
  416. for (i = 0; i < 2; i++) {
  417. if (!(fifo->iso[i].purb)) {
  418. fifo->iso[i].purb =
  419. usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
  420. if (!(fifo->iso[i].purb)) {
  421. printk(KERN_INFO
  422. "alloc urb for fifo %i failed!!!",
  423. fifo->fifonum);
  424. }
  425. fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
  426. /* Init the first iso */
  427. if (ISO_BUFFER_SIZE >=
  428. (fifo->usb_packet_maxlen *
  429. num_packets_per_urb)) {
  430. fill_isoc_urb(fifo->iso[i].purb,
  431. fifo->hfc->dev, fifo->pipe,
  432. fifo->iso[i].buffer,
  433. num_packets_per_urb,
  434. fifo->usb_packet_maxlen,
  435. fifo->intervall, complete,
  436. &fifo->iso[i]);
  437. memset(fifo->iso[i].buffer, 0,
  438. sizeof(fifo->iso[i].buffer));
  439. /* defining packet delimeters in fifo->buffer */
  440. for (k = 0; k < num_packets_per_urb; k++) {
  441. fifo->iso[i].purb->
  442. iso_frame_desc[k].offset =
  443. k * packet_size;
  444. fifo->iso[i].purb->
  445. iso_frame_desc[k].length =
  446. packet_size;
  447. }
  448. } else {
  449. printk(KERN_INFO
  450. "HFC-S USB: ISO Buffer size to small!\n");
  451. }
  452. }
  453. fifo->bit_line = BITLINE_INF;
  454. errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
  455. fifo->active = (errcode >= 0) ? 1 : 0;
  456. if (errcode < 0)
  457. printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
  458. i, errcode, symbolic(urb_errlist, errcode));
  459. }
  460. return (fifo->active);
  461. }
  462. /* stops running iso chain and frees their pending urbs */
  463. static void
  464. stop_isoc_chain(usb_fifo * fifo)
  465. {
  466. int i;
  467. for (i = 0; i < 2; i++) {
  468. if (fifo->iso[i].purb) {
  469. DBG(HFCUSB_DBG_INIT,
  470. "HFC-S USB: Stopping iso chain for fifo %i.%i",
  471. fifo->fifonum, i);
  472. usb_kill_urb(fifo->iso[i].purb);
  473. usb_free_urb(fifo->iso[i].purb);
  474. fifo->iso[i].purb = NULL;
  475. }
  476. }
  477. usb_kill_urb(fifo->urb);
  478. usb_free_urb(fifo->urb);
  479. fifo->urb = NULL;
  480. fifo->active = 0;
  481. }
  482. /* defines how much ISO packets are handled in one URB */
  483. static int iso_packets[8] =
  484. { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
  485. ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
  486. };
  487. static void
  488. tx_iso_complete(struct urb *urb)
  489. {
  490. iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
  491. usb_fifo *fifo = context_iso_urb->owner_fifo;
  492. hfcusb_data *hfc = fifo->hfc;
  493. int k, tx_offset, num_isoc_packets, sink, len, current_len,
  494. errcode;
  495. int frame_complete, transp_mode, fifon, status;
  496. __u8 threshbit;
  497. fifon = fifo->fifonum;
  498. status = urb->status;
  499. tx_offset = 0;
  500. /* ISO transfer only partially completed,
  501. look at individual frame status for details */
  502. if (status == -EXDEV) {
  503. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
  504. ", urb->status %d, fifonum %d\n",
  505. status, fifon);
  506. for (k = 0; k < iso_packets[fifon]; ++k) {
  507. errcode = urb->iso_frame_desc[k].status;
  508. if (errcode)
  509. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
  510. "packet %i, status: %i\n",
  511. k, errcode);
  512. }
  513. // clear status, so go on with ISO transfers
  514. status = 0;
  515. }
  516. if (fifo->active && !status) {
  517. transp_mode = 0;
  518. if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
  519. transp_mode = 1;
  520. /* is FifoFull-threshold set for our channel? */
  521. threshbit = (hfc->threshold_mask & (1 << fifon));
  522. num_isoc_packets = iso_packets[fifon];
  523. /* predict dataflow to avoid fifo overflow */
  524. if (fifon >= HFCUSB_D_TX) {
  525. sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
  526. } else {
  527. sink = (threshbit) ? SINK_MIN : SINK_MAX;
  528. }
  529. fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
  530. context_iso_urb->buffer, num_isoc_packets,
  531. fifo->usb_packet_maxlen, fifo->intervall,
  532. tx_iso_complete, urb->context);
  533. memset(context_iso_urb->buffer, 0,
  534. sizeof(context_iso_urb->buffer));
  535. frame_complete = 0;
  536. /* Generate next ISO Packets */
  537. for (k = 0; k < num_isoc_packets; ++k) {
  538. if (fifo->skbuff) {
  539. len = fifo->skbuff->len;
  540. /* we lower data margin every msec */
  541. fifo->bit_line -= sink;
  542. current_len = (0 - fifo->bit_line) / 8;
  543. /* maximum 15 byte for every ISO packet makes our life easier */
  544. if (current_len > 14)
  545. current_len = 14;
  546. current_len =
  547. (len <=
  548. current_len) ? len : current_len;
  549. /* how much bit do we put on the line? */
  550. fifo->bit_line += current_len * 8;
  551. context_iso_urb->buffer[tx_offset] = 0;
  552. if (current_len == len) {
  553. if (!transp_mode) {
  554. /* here frame completion */
  555. context_iso_urb->
  556. buffer[tx_offset] = 1;
  557. /* add 2 byte flags and 16bit CRC at end of ISDN frame */
  558. fifo->bit_line += 32;
  559. }
  560. frame_complete = 1;
  561. }
  562. memcpy(context_iso_urb->buffer +
  563. tx_offset + 1, fifo->skbuff->data,
  564. current_len);
  565. skb_pull(fifo->skbuff, current_len);
  566. /* define packet delimeters within the URB buffer */
  567. urb->iso_frame_desc[k].offset = tx_offset;
  568. urb->iso_frame_desc[k].length =
  569. current_len + 1;
  570. tx_offset += (current_len + 1);
  571. } else {
  572. urb->iso_frame_desc[k].offset =
  573. tx_offset++;
  574. urb->iso_frame_desc[k].length = 1;
  575. fifo->bit_line -= sink; /* we lower data margin every msec */
  576. if (fifo->bit_line < BITLINE_INF) {
  577. fifo->bit_line = BITLINE_INF;
  578. }
  579. }
  580. if (frame_complete) {
  581. fifo->delete_flg = 1;
  582. fifo->hif->l1l2(fifo->hif,
  583. PH_DATA | CONFIRM,
  584. (void *) (unsigned long) fifo->skbuff->
  585. truesize);
  586. if (fifo->skbuff && fifo->delete_flg) {
  587. dev_kfree_skb_any(fifo->skbuff);
  588. fifo->skbuff = NULL;
  589. fifo->delete_flg = 0;
  590. }
  591. frame_complete = 0;
  592. }
  593. }
  594. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  595. if (errcode < 0) {
  596. printk(KERN_INFO
  597. "HFC-S USB: error submitting ISO URB: %d\n",
  598. errcode);
  599. }
  600. } else {
  601. if (status && !hfc->disc_flag) {
  602. printk(KERN_INFO
  603. "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
  604. status, symbolic(urb_errlist, status), fifon);
  605. }
  606. }
  607. }
  608. static void
  609. rx_iso_complete(struct urb *urb)
  610. {
  611. iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
  612. usb_fifo *fifo = context_iso_urb->owner_fifo;
  613. hfcusb_data *hfc = fifo->hfc;
  614. int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
  615. status;
  616. unsigned int iso_status;
  617. __u8 *buf;
  618. static __u8 eof[8];
  619. fifon = fifo->fifonum;
  620. status = urb->status;
  621. if (urb->status == -EOVERFLOW) {
  622. DBG(HFCUSB_DBG_VERBOSE_USB,
  623. "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
  624. status = 0;
  625. }
  626. /* ISO transfer only partially completed,
  627. look at individual frame status for details */
  628. if (status == -EXDEV) {
  629. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
  630. "urb->status %d, fifonum %d\n",
  631. status, fifon);
  632. status = 0;
  633. }
  634. if (fifo->active && !status) {
  635. num_isoc_packets = iso_packets[fifon];
  636. maxlen = fifo->usb_packet_maxlen;
  637. for (k = 0; k < num_isoc_packets; ++k) {
  638. len = urb->iso_frame_desc[k].actual_length;
  639. offset = urb->iso_frame_desc[k].offset;
  640. buf = context_iso_urb->buffer + offset;
  641. iso_status = urb->iso_frame_desc[k].status;
  642. if (iso_status && !hfc->disc_flag)
  643. DBG(HFCUSB_DBG_VERBOSE_USB,
  644. "HFC-S USB: rx_iso_complete "
  645. "ISO packet %i, status: %i\n",
  646. k, iso_status);
  647. if (fifon == HFCUSB_D_RX) {
  648. DBG(HFCUSB_DBG_VERBOSE_USB,
  649. "HFC-S USB: ISO-D-RX lst_urblen:%2d "
  650. "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
  651. fifo->last_urblen, len, maxlen,
  652. eof[5]);
  653. DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
  654. }
  655. if (fifo->last_urblen != maxlen) {
  656. /* the threshold mask is in the 2nd status byte */
  657. hfc->threshold_mask = buf[1];
  658. /* care for L1 state only for D-Channel
  659. to avoid overlapped iso completions */
  660. if (fifon == HFCUSB_D_RX) {
  661. /* the S0 state is in the upper half
  662. of the 1st status byte */
  663. s0_state_handler(hfc, buf[0] >> 4);
  664. }
  665. eof[fifon] = buf[0] & 1;
  666. if (len > 2)
  667. collect_rx_frame(fifo, buf + 2,
  668. len - 2,
  669. (len < maxlen) ?
  670. eof[fifon] : 0);
  671. } else {
  672. collect_rx_frame(fifo, buf, len,
  673. (len <
  674. maxlen) ? eof[fifon] :
  675. 0);
  676. }
  677. fifo->last_urblen = len;
  678. }
  679. fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
  680. context_iso_urb->buffer, num_isoc_packets,
  681. fifo->usb_packet_maxlen, fifo->intervall,
  682. rx_iso_complete, urb->context);
  683. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  684. if (errcode < 0) {
  685. printk(KERN_ERR
  686. "HFC-S USB: error submitting ISO URB: %d\n",
  687. errcode);
  688. }
  689. } else {
  690. if (status && !hfc->disc_flag) {
  691. printk(KERN_ERR
  692. "HFC-S USB: rx_iso_complete : "
  693. "urb->status %d, fifonum %d\n",
  694. status, fifon);
  695. }
  696. }
  697. }
  698. /* collect rx data from INT- and ISO-URBs */
  699. static void
  700. collect_rx_frame(usb_fifo * fifo, __u8 * data, int len, int finish)
  701. {
  702. hfcusb_data *hfc = fifo->hfc;
  703. int transp_mode, fifon;
  704. fifon = fifo->fifonum;
  705. transp_mode = 0;
  706. if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
  707. transp_mode = 1;
  708. if (!fifo->skbuff) {
  709. fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
  710. if (!fifo->skbuff) {
  711. printk(KERN_ERR
  712. "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
  713. fifon);
  714. return;
  715. }
  716. }
  717. if (len) {
  718. if (fifo->skbuff->len + len < fifo->max_size) {
  719. memcpy(skb_put(fifo->skbuff, len), data, len);
  720. } else {
  721. DBG(HFCUSB_DBG_FIFO_ERR,
  722. "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
  723. fifo->max_size, fifon);
  724. DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
  725. skb_trim(fifo->skbuff, 0);
  726. }
  727. }
  728. if (transp_mode && fifo->skbuff->len >= 128) {
  729. fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
  730. fifo->skbuff);
  731. fifo->skbuff = NULL;
  732. return;
  733. }
  734. /* we have a complete hdlc packet */
  735. if (finish) {
  736. if ((!fifo->skbuff->data[fifo->skbuff->len - 1])
  737. && (fifo->skbuff->len > 3)) {
  738. if (fifon == HFCUSB_D_RX) {
  739. DBG(HFCUSB_DBG_DCHANNEL,
  740. "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
  741. DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
  742. }
  743. /* remove CRC & status */
  744. skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
  745. if (fifon == HFCUSB_PCM_RX) {
  746. fifo->hif->l1l2(fifo->hif,
  747. PH_DATA_E | INDICATION,
  748. fifo->skbuff);
  749. } else
  750. fifo->hif->l1l2(fifo->hif,
  751. PH_DATA | INDICATION,
  752. fifo->skbuff);
  753. fifo->skbuff = NULL; /* buffer was freed from upper layer */
  754. } else {
  755. DBG(HFCUSB_DBG_FIFO_ERR,
  756. "HFC-S USB: ERROR frame len(%d) fifo(%d)",
  757. fifo->skbuff->len, fifon);
  758. DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
  759. skb_trim(fifo->skbuff, 0);
  760. }
  761. }
  762. }
  763. static void
  764. rx_int_complete(struct urb *urb)
  765. {
  766. int len;
  767. int status;
  768. __u8 *buf, maxlen, fifon;
  769. usb_fifo *fifo = (usb_fifo *) urb->context;
  770. hfcusb_data *hfc = fifo->hfc;
  771. static __u8 eof[8];
  772. urb->dev = hfc->dev; /* security init */
  773. fifon = fifo->fifonum;
  774. if ((!fifo->active) || (urb->status)) {
  775. DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
  776. fifon, urb->status);
  777. fifo->urb->interval = 0; /* cancel automatic rescheduling */
  778. if (fifo->skbuff) {
  779. dev_kfree_skb_any(fifo->skbuff);
  780. fifo->skbuff = NULL;
  781. }
  782. return;
  783. }
  784. len = urb->actual_length;
  785. buf = fifo->buffer;
  786. maxlen = fifo->usb_packet_maxlen;
  787. if (fifon == HFCUSB_D_RX) {
  788. DBG(HFCUSB_DBG_VERBOSE_USB,
  789. "HFC-S USB: INT-D-RX lst_urblen:%2d "
  790. "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
  791. fifo->last_urblen, len, maxlen,
  792. eof[5]);
  793. DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
  794. }
  795. if (fifo->last_urblen != fifo->usb_packet_maxlen) {
  796. /* the threshold mask is in the 2nd status byte */
  797. hfc->threshold_mask = buf[1];
  798. /* the S0 state is in the upper half of the 1st status byte */
  799. s0_state_handler(hfc, buf[0] >> 4);
  800. eof[fifon] = buf[0] & 1;
  801. /* if we have more than the 2 status bytes -> collect data */
  802. if (len > 2)
  803. collect_rx_frame(fifo, buf + 2,
  804. urb->actual_length - 2,
  805. (len < maxlen) ? eof[fifon] : 0);
  806. } else {
  807. collect_rx_frame(fifo, buf, urb->actual_length,
  808. (len < maxlen) ? eof[fifon] : 0);
  809. }
  810. fifo->last_urblen = urb->actual_length;
  811. status = usb_submit_urb(urb, GFP_ATOMIC);
  812. if (status) {
  813. printk(KERN_INFO
  814. "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
  815. __func__, fifon);
  816. }
  817. }
  818. /* start initial INT-URB for certain fifo */
  819. static void
  820. start_int_fifo(usb_fifo * fifo)
  821. {
  822. int errcode;
  823. DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
  824. fifo->fifonum);
  825. if (!fifo->urb) {
  826. fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
  827. if (!fifo->urb)
  828. return;
  829. }
  830. usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
  831. fifo->buffer, fifo->usb_packet_maxlen,
  832. rx_int_complete, fifo, fifo->intervall);
  833. fifo->active = 1; /* must be marked active */
  834. errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
  835. if (errcode) {
  836. printk(KERN_ERR
  837. "HFC-S USB: submit URB error(start_int_info): status:%i\n",
  838. errcode);
  839. fifo->active = 0;
  840. fifo->skbuff = NULL;
  841. }
  842. }
  843. static void
  844. setup_bchannel(hfcusb_data * hfc, int channel, int mode)
  845. {
  846. __u8 val, idx_table[2] = { 0, 2 };
  847. if (hfc->disc_flag) {
  848. return;
  849. }
  850. DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
  851. channel, mode);
  852. hfc->b_mode[channel] = mode;
  853. /* setup CON_HDLC */
  854. val = 0;
  855. if (mode != L1_MODE_NULL)
  856. val = 8; /* enable fifo? */
  857. if (mode == L1_MODE_TRANS)
  858. val |= 2; /* set transparent bit */
  859. /* set FIFO to transmit register */
  860. queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 1);
  861. queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
  862. /* reset fifo */
  863. queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
  864. /* set FIFO to receive register */
  865. queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
  866. queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
  867. /* reset fifo */
  868. queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
  869. val = 0x40;
  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, val, 1);
  875. val = 0;
  876. if (hfc->b_mode[0])
  877. val |= 1;
  878. if (hfc->b_mode[1])
  879. val |= 2;
  880. queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);
  881. if (mode == L1_MODE_NULL) {
  882. if (channel)
  883. handle_led(hfc, LED_B2_OFF);
  884. else
  885. handle_led(hfc, LED_B1_OFF);
  886. } else {
  887. if (channel)
  888. handle_led(hfc, LED_B2_ON);
  889. else
  890. handle_led(hfc, LED_B1_ON);
  891. }
  892. }
  893. static void
  894. hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
  895. {
  896. usb_fifo *fifo = my_hisax_if->priv;
  897. hfcusb_data *hfc = fifo->hfc;
  898. switch (pr) {
  899. case PH_ACTIVATE | REQUEST:
  900. if (fifo->fifonum == HFCUSB_D_TX) {
  901. DBG(HFCUSB_DBG_STATES,
  902. "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
  903. if (hfc->l1_state != 3
  904. && hfc->l1_state != 7) {
  905. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
  906. PH_DEACTIVATE |
  907. INDICATION,
  908. NULL);
  909. DBG(HFCUSB_DBG_STATES,
  910. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
  911. } else {
  912. if (hfc->l1_state == 7) { /* l1 already active */
  913. hfc->d_if.ifc.l1l2(&hfc->
  914. d_if.
  915. ifc,
  916. PH_ACTIVATE
  917. |
  918. INDICATION,
  919. NULL);
  920. DBG(HFCUSB_DBG_STATES,
  921. "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
  922. } else {
  923. /* force sending sending INFO1 */
  924. queue_control_request(hfc,
  925. HFCUSB_STATES,
  926. 0x14,
  927. 1);
  928. mdelay(1);
  929. /* start l1 activation */
  930. queue_control_request(hfc,
  931. HFCUSB_STATES,
  932. 0x04,
  933. 1);
  934. if (!timer_pending
  935. (&hfc->t3_timer)) {
  936. hfc->t3_timer.
  937. expires =
  938. jiffies +
  939. (HFC_TIMER_T3 *
  940. HZ) / 1000;
  941. add_timer(&hfc->
  942. t3_timer);
  943. }
  944. }
  945. }
  946. } else {
  947. DBG(HFCUSB_DBG_STATES,
  948. "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
  949. setup_bchannel(hfc,
  950. (fifo->fifonum ==
  951. HFCUSB_B1_TX) ? 0 : 1,
  952. (long) arg);
  953. fifo->hif->l1l2(fifo->hif,
  954. PH_ACTIVATE | INDICATION,
  955. NULL);
  956. }
  957. break;
  958. case PH_DEACTIVATE | REQUEST:
  959. if (fifo->fifonum == HFCUSB_D_TX) {
  960. DBG(HFCUSB_DBG_STATES,
  961. "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
  962. } else {
  963. DBG(HFCUSB_DBG_STATES,
  964. "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
  965. setup_bchannel(hfc,
  966. (fifo->fifonum ==
  967. HFCUSB_B1_TX) ? 0 : 1,
  968. (int) L1_MODE_NULL);
  969. fifo->hif->l1l2(fifo->hif,
  970. PH_DEACTIVATE | INDICATION,
  971. NULL);
  972. }
  973. break;
  974. case PH_DATA | REQUEST:
  975. if (fifo->skbuff && fifo->delete_flg) {
  976. dev_kfree_skb_any(fifo->skbuff);
  977. fifo->skbuff = NULL;
  978. fifo->delete_flg = 0;
  979. }
  980. fifo->skbuff = arg; /* we have a new buffer */
  981. break;
  982. default:
  983. DBG(HFCUSB_DBG_STATES,
  984. "HFC_USB: hfc_usb_d_l2l1: unkown state : %#x", pr);
  985. break;
  986. }
  987. }
  988. /* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
  989. static int
  990. hfc_usb_init(hfcusb_data * hfc)
  991. {
  992. usb_fifo *fifo;
  993. int i, err;
  994. u_char b;
  995. struct hisax_b_if *p_b_if[2];
  996. /* check the chip id */
  997. if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
  998. printk(KERN_INFO "HFC-USB: cannot read chip id\n");
  999. return (1);
  1000. }
  1001. if (b != HFCUSB_CHIPID) {
  1002. printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
  1003. return (1);
  1004. }
  1005. /* first set the needed config, interface and alternate */
  1006. err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
  1007. /* do Chip reset */
  1008. write_usb(hfc, HFCUSB_CIRM, 8);
  1009. /* aux = output, reset off */
  1010. write_usb(hfc, HFCUSB_CIRM, 0x10);
  1011. /* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
  1012. write_usb(hfc, HFCUSB_USB_SIZE,
  1013. (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));
  1014. /* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
  1015. write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
  1016. /* enable PCM/GCI master mode */
  1017. write_usb(hfc, HFCUSB_MST_MODE1, 0); /* set default values */
  1018. write_usb(hfc, HFCUSB_MST_MODE0, 1); /* enable master mode */
  1019. /* init the fifos */
  1020. write_usb(hfc, HFCUSB_F_THRES,
  1021. (HFCUSB_TX_THRESHOLD /
  1022. 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
  1023. fifo = hfc->fifos;
  1024. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1025. write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
  1026. fifo[i].skbuff = NULL; /* init buffer pointer */
  1027. fifo[i].max_size =
  1028. (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
  1029. fifo[i].last_urblen = 0;
  1030. /* set 2 bit for D- & E-channel */
  1031. write_usb(hfc, HFCUSB_HDLC_PAR,
  1032. ((i <= HFCUSB_B2_RX) ? 0 : 2));
  1033. /* rx hdlc, enable IFF for D-channel */
  1034. write_usb(hfc, HFCUSB_CON_HDLC,
  1035. ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
  1036. write_usb(hfc, HFCUSB_INC_RES_F, 2); /* reset the fifo */
  1037. }
  1038. write_usb(hfc, HFCUSB_CLKDEL, 0x0f); /* clock delay value */
  1039. write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
  1040. write_usb(hfc, HFCUSB_STATES, 3); /* enable state machine */
  1041. write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
  1042. write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
  1043. /* set both B-channel to not connected */
  1044. hfc->b_mode[0] = L1_MODE_NULL;
  1045. hfc->b_mode[1] = L1_MODE_NULL;
  1046. hfc->l1_activated = 0;
  1047. hfc->disc_flag = 0;
  1048. hfc->led_state = 0;
  1049. hfc->old_led_state = 0;
  1050. /* init the t3 timer */
  1051. init_timer(&hfc->t3_timer);
  1052. hfc->t3_timer.data = (long) hfc;
  1053. hfc->t3_timer.function = (void *) l1_timer_expire_t3;
  1054. /* init the t4 timer */
  1055. init_timer(&hfc->t4_timer);
  1056. hfc->t4_timer.data = (long) hfc;
  1057. hfc->t4_timer.function = (void *) l1_timer_expire_t4;
  1058. /* init the background machinery for control requests */
  1059. hfc->ctrl_read.bRequestType = 0xc0;
  1060. hfc->ctrl_read.bRequest = 1;
  1061. hfc->ctrl_read.wLength = cpu_to_le16(1);
  1062. hfc->ctrl_write.bRequestType = 0x40;
  1063. hfc->ctrl_write.bRequest = 0;
  1064. hfc->ctrl_write.wLength = 0;
  1065. usb_fill_control_urb(hfc->ctrl_urb,
  1066. hfc->dev,
  1067. hfc->ctrl_out_pipe,
  1068. (u_char *) & hfc->ctrl_write,
  1069. NULL, 0, ctrl_complete, hfc);
  1070. /* Init All Fifos */
  1071. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1072. hfc->fifos[i].iso[0].purb = NULL;
  1073. hfc->fifos[i].iso[1].purb = NULL;
  1074. hfc->fifos[i].active = 0;
  1075. }
  1076. /* register Modul to upper Hisax Layers */
  1077. hfc->d_if.owner = THIS_MODULE;
  1078. hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
  1079. hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
  1080. for (i = 0; i < 2; i++) {
  1081. hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
  1082. hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
  1083. p_b_if[i] = &hfc->b_if[i];
  1084. }
  1085. /* default Prot: EURO ISDN, should be a module_param */
  1086. hfc->protocol = 2;
  1087. i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
  1088. if (i) {
  1089. printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
  1090. return i;
  1091. }
  1092. #ifdef CONFIG_HISAX_DEBUG
  1093. hfc_debug = debug;
  1094. #endif
  1095. for (i = 0; i < 4; i++)
  1096. hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
  1097. for (i = 4; i < 8; i++)
  1098. hfc->fifos[i].hif = &hfc->d_if.ifc;
  1099. /* 3 (+1) INT IN + 3 ISO OUT */
  1100. if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
  1101. start_int_fifo(hfc->fifos + HFCUSB_D_RX);
  1102. if (hfc->fifos[HFCUSB_PCM_RX].pipe)
  1103. start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
  1104. start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
  1105. start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
  1106. }
  1107. /* 3 (+1) ISO IN + 3 ISO OUT */
  1108. if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
  1109. start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
  1110. rx_iso_complete, 16);
  1111. if (hfc->fifos[HFCUSB_PCM_RX].pipe)
  1112. start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
  1113. ISOC_PACKETS_D, rx_iso_complete,
  1114. 16);
  1115. start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
  1116. rx_iso_complete, 16);
  1117. start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
  1118. rx_iso_complete, 16);
  1119. }
  1120. start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
  1121. tx_iso_complete, 1);
  1122. start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
  1123. tx_iso_complete, 1);
  1124. start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
  1125. tx_iso_complete, 1);
  1126. handle_led(hfc, LED_POWER_ON);
  1127. return (0);
  1128. }
  1129. /* initial callback for each plugged USB device */
  1130. static int
  1131. hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1132. {
  1133. struct usb_device *dev = interface_to_usbdev(intf);
  1134. hfcusb_data *context;
  1135. struct usb_host_interface *iface = intf->cur_altsetting;
  1136. struct usb_host_interface *iface_used = NULL;
  1137. struct usb_host_endpoint *ep;
  1138. int ifnum = iface->desc.bInterfaceNumber;
  1139. int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
  1140. attr, cfg_found, cidx, ep_addr;
  1141. int cmptbl[16], small_match, iso_packet_size, packet_size,
  1142. alt_used = 0;
  1143. hfcsusb_vdata *driver_info;
  1144. vend_idx = 0xffff;
  1145. for (i = 0; hfcusb_idtab[i].idVendor; i++) {
  1146. if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
  1147. && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
  1148. vend_idx = i;
  1149. continue;
  1150. }
  1151. }
  1152. printk(KERN_INFO
  1153. "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
  1154. ifnum, iface->desc.bAlternateSetting, intf->minor);
  1155. if (vend_idx != 0xffff) {
  1156. /* if vendor and product ID is OK, start probing alternate settings */
  1157. alt_idx = 0;
  1158. small_match = 0xffff;
  1159. /* default settings */
  1160. iso_packet_size = 16;
  1161. packet_size = 64;
  1162. while (alt_idx < intf->num_altsetting) {
  1163. iface = intf->altsetting + alt_idx;
  1164. probe_alt_setting = iface->desc.bAlternateSetting;
  1165. cfg_used = 0;
  1166. /* check for config EOL element */
  1167. while (validconf[cfg_used][0]) {
  1168. cfg_found = 1;
  1169. vcf = validconf[cfg_used];
  1170. /* first endpoint descriptor */
  1171. ep = iface->endpoint;
  1172. memcpy(cmptbl, vcf, 16 * sizeof(int));
  1173. /* check for all endpoints in this alternate setting */
  1174. for (i = 0; i < iface->desc.bNumEndpoints;
  1175. i++) {
  1176. ep_addr =
  1177. ep->desc.bEndpointAddress;
  1178. /* get endpoint base */
  1179. idx = ((ep_addr & 0x7f) - 1) * 2;
  1180. if (ep_addr & 0x80)
  1181. idx++;
  1182. attr = ep->desc.bmAttributes;
  1183. if (cmptbl[idx] == EP_NUL) {
  1184. cfg_found = 0;
  1185. }
  1186. if (attr == USB_ENDPOINT_XFER_INT
  1187. && cmptbl[idx] == EP_INT)
  1188. cmptbl[idx] = EP_NUL;
  1189. if (attr == USB_ENDPOINT_XFER_BULK
  1190. && cmptbl[idx] == EP_BLK)
  1191. cmptbl[idx] = EP_NUL;
  1192. if (attr == USB_ENDPOINT_XFER_ISOC
  1193. && cmptbl[idx] == EP_ISO)
  1194. cmptbl[idx] = EP_NUL;
  1195. /* check if all INT endpoints match minimum interval */
  1196. if ((attr == USB_ENDPOINT_XFER_INT)
  1197. && (ep->desc.bInterval < vcf[17])) {
  1198. cfg_found = 0;
  1199. }
  1200. ep++;
  1201. }
  1202. for (i = 0; i < 16; i++) {
  1203. /* all entries must be EP_NOP or EP_NUL for a valid config */
  1204. if (cmptbl[i] != EP_NOP
  1205. && cmptbl[i] != EP_NUL)
  1206. cfg_found = 0;
  1207. }
  1208. if (cfg_found) {
  1209. if (cfg_used < small_match) {
  1210. small_match = cfg_used;
  1211. alt_used =
  1212. probe_alt_setting;
  1213. iface_used = iface;
  1214. }
  1215. }
  1216. cfg_used++;
  1217. }
  1218. alt_idx++;
  1219. } /* (alt_idx < intf->num_altsetting) */
  1220. /* found a valid USB Ta Endpint config */
  1221. if (small_match != 0xffff) {
  1222. iface = iface_used;
  1223. if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
  1224. return (-ENOMEM); /* got no mem */
  1225. ep = iface->endpoint;
  1226. vcf = validconf[small_match];
  1227. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1228. ep_addr = ep->desc.bEndpointAddress;
  1229. /* get endpoint base */
  1230. idx = ((ep_addr & 0x7f) - 1) * 2;
  1231. if (ep_addr & 0x80)
  1232. idx++;
  1233. cidx = idx & 7;
  1234. attr = ep->desc.bmAttributes;
  1235. /* init Endpoints */
  1236. if (vcf[idx] != EP_NOP
  1237. && vcf[idx] != EP_NUL) {
  1238. switch (attr) {
  1239. case USB_ENDPOINT_XFER_INT:
  1240. context->
  1241. fifos[cidx].
  1242. pipe =
  1243. usb_rcvintpipe
  1244. (dev,
  1245. ep->desc.
  1246. bEndpointAddress);
  1247. context->
  1248. fifos[cidx].
  1249. usb_transfer_mode
  1250. = USB_INT;
  1251. packet_size =
  1252. le16_to_cpu(ep->desc.wMaxPacketSize);
  1253. break;
  1254. case USB_ENDPOINT_XFER_BULK:
  1255. if (ep_addr & 0x80)
  1256. context->
  1257. fifos
  1258. [cidx].
  1259. pipe =
  1260. usb_rcvbulkpipe
  1261. (dev,
  1262. ep->
  1263. desc.
  1264. bEndpointAddress);
  1265. else
  1266. context->
  1267. fifos
  1268. [cidx].
  1269. pipe =
  1270. usb_sndbulkpipe
  1271. (dev,
  1272. ep->
  1273. desc.
  1274. bEndpointAddress);
  1275. context->
  1276. fifos[cidx].
  1277. usb_transfer_mode
  1278. = USB_BULK;
  1279. packet_size =
  1280. le16_to_cpu(ep->desc.wMaxPacketSize);
  1281. break;
  1282. case USB_ENDPOINT_XFER_ISOC:
  1283. if (ep_addr & 0x80)
  1284. context->
  1285. fifos
  1286. [cidx].
  1287. pipe =
  1288. usb_rcvisocpipe
  1289. (dev,
  1290. ep->
  1291. desc.
  1292. bEndpointAddress);
  1293. else
  1294. context->
  1295. fifos
  1296. [cidx].
  1297. pipe =
  1298. usb_sndisocpipe
  1299. (dev,
  1300. ep->
  1301. desc.
  1302. bEndpointAddress);
  1303. context->
  1304. fifos[cidx].
  1305. usb_transfer_mode
  1306. = USB_ISOC;
  1307. iso_packet_size =
  1308. le16_to_cpu(ep->desc.wMaxPacketSize);
  1309. break;
  1310. default:
  1311. context->
  1312. fifos[cidx].
  1313. pipe = 0;
  1314. } /* switch attribute */
  1315. if (context->fifos[cidx].pipe) {
  1316. context->fifos[cidx].
  1317. fifonum = cidx;
  1318. context->fifos[cidx].hfc =
  1319. context;
  1320. context->fifos[cidx].usb_packet_maxlen =
  1321. le16_to_cpu(ep->desc.wMaxPacketSize);
  1322. context->fifos[cidx].
  1323. intervall =
  1324. ep->desc.bInterval;
  1325. context->fifos[cidx].
  1326. skbuff = NULL;
  1327. }
  1328. }
  1329. ep++;
  1330. }
  1331. context->dev = dev; /* save device */
  1332. context->if_used = ifnum; /* save used interface */
  1333. context->alt_used = alt_used; /* and alternate config */
  1334. context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
  1335. context->cfg_used = vcf[16]; /* store used config */
  1336. context->vend_idx = vend_idx; /* store found vendor */
  1337. context->packet_size = packet_size;
  1338. context->iso_packet_size = iso_packet_size;
  1339. /* create the control pipes needed for register access */
  1340. context->ctrl_in_pipe =
  1341. usb_rcvctrlpipe(context->dev, 0);
  1342. context->ctrl_out_pipe =
  1343. usb_sndctrlpipe(context->dev, 0);
  1344. context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  1345. driver_info =
  1346. (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
  1347. driver_info;
  1348. printk(KERN_INFO "HFC-S USB: detected \"%s\"\n",
  1349. driver_info->vend_name);
  1350. DBG(HFCUSB_DBG_INIT,
  1351. "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
  1352. conf_str[small_match], context->if_used,
  1353. context->alt_used,
  1354. validconf[small_match][18]);
  1355. /* init the chip and register the driver */
  1356. if (hfc_usb_init(context)) {
  1357. usb_kill_urb(context->ctrl_urb);
  1358. usb_free_urb(context->ctrl_urb);
  1359. context->ctrl_urb = NULL;
  1360. kfree(context);
  1361. return (-EIO);
  1362. }
  1363. usb_set_intfdata(intf, context);
  1364. return (0);
  1365. }
  1366. } else {
  1367. printk(KERN_INFO
  1368. "HFC-S USB: no valid vendor found in USB descriptor\n");
  1369. }
  1370. return (-EIO);
  1371. }
  1372. /* callback for unplugged USB device */
  1373. static void
  1374. hfc_usb_disconnect(struct usb_interface *intf)
  1375. {
  1376. hfcusb_data *context = usb_get_intfdata(intf);
  1377. int i;
  1378. handle_led(context, LED_POWER_OFF);
  1379. schedule_timeout(HZ / 100);
  1380. printk(KERN_INFO "HFC-S USB: device disconnect\n");
  1381. context->disc_flag = 1;
  1382. usb_set_intfdata(intf, NULL);
  1383. if (timer_pending(&context->t3_timer))
  1384. del_timer(&context->t3_timer);
  1385. if (timer_pending(&context->t4_timer))
  1386. del_timer(&context->t4_timer);
  1387. /* tell all fifos to terminate */
  1388. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1389. if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
  1390. if (context->fifos[i].active > 0) {
  1391. stop_isoc_chain(&context->fifos[i]);
  1392. DBG(HFCUSB_DBG_INIT,
  1393. "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
  1394. __func__, i);
  1395. }
  1396. } else {
  1397. if (context->fifos[i].active > 0) {
  1398. context->fifos[i].active = 0;
  1399. DBG(HFCUSB_DBG_INIT,
  1400. "HFC-S USB: %s unlinking URB for Fifo(%i)",
  1401. __func__, i);
  1402. }
  1403. usb_kill_urb(context->fifos[i].urb);
  1404. usb_free_urb(context->fifos[i].urb);
  1405. context->fifos[i].urb = NULL;
  1406. }
  1407. context->fifos[i].active = 0;
  1408. }
  1409. usb_kill_urb(context->ctrl_urb);
  1410. usb_free_urb(context->ctrl_urb);
  1411. context->ctrl_urb = NULL;
  1412. hisax_unregister(&context->d_if);
  1413. kfree(context); /* free our structure again */
  1414. }
  1415. static struct usb_driver hfc_drv = {
  1416. .name = "hfc_usb",
  1417. .id_table = hfcusb_idtab,
  1418. .probe = hfc_usb_probe,
  1419. .disconnect = hfc_usb_disconnect,
  1420. };
  1421. static void __exit
  1422. hfc_usb_mod_exit(void)
  1423. {
  1424. usb_deregister(&hfc_drv); /* release our driver */
  1425. printk(KERN_INFO "HFC-S USB: module removed\n");
  1426. }
  1427. static int __init
  1428. hfc_usb_mod_init(void)
  1429. {
  1430. char revstr[30], datestr[30], dummy[30];
  1431. #ifndef CONFIG_HISAX_DEBUG
  1432. hfc_debug = debug;
  1433. #endif
  1434. sscanf(hfcusb_revision,
  1435. "%s %s $ %s %s %s $ ", dummy, revstr,
  1436. dummy, datestr, dummy);
  1437. printk(KERN_INFO
  1438. "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
  1439. revstr, datestr, debug);
  1440. if (usb_register(&hfc_drv)) {
  1441. printk(KERN_INFO
  1442. "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
  1443. return (-1); /* unable to register */
  1444. }
  1445. return (0);
  1446. }
  1447. module_init(hfc_usb_mod_init);
  1448. module_exit(hfc_usb_mod_exit);
  1449. MODULE_AUTHOR(DRIVER_AUTHOR);
  1450. MODULE_DESCRIPTION(DRIVER_DESC);
  1451. MODULE_LICENSE("GPL");
  1452. MODULE_DEVICE_TABLE(usb, hfcusb_idtab);