hfc_usb.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. /*
  2. * hfc_usb.c
  3. *
  4. * $Id: hfc_usb.c,v 2.3.2.20 2007/08/20 14:07:54 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.20 $ $Date: 2007/08/20 14:07:54 $ ";
  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. };
  125. /* structure defining input+output fifos (interrupt/bulk mode) */
  126. struct usb_fifo; /* forward definition */
  127. typedef struct iso_urb_struct {
  128. struct urb *purb;
  129. __u8 buffer[ISO_BUFFER_SIZE]; /* buffer incoming/outgoing data */
  130. struct usb_fifo *owner_fifo; /* pointer to owner fifo */
  131. } iso_urb_struct;
  132. struct hfcusb_data; /* forward definition */
  133. typedef struct usb_fifo {
  134. int fifonum; /* fifo index attached to this structure */
  135. int active; /* fifo is currently active */
  136. struct hfcusb_data *hfc; /* pointer to main structure */
  137. int pipe; /* address of endpoint */
  138. __u8 usb_packet_maxlen; /* maximum length for usb transfer */
  139. unsigned int max_size; /* maximum size of receive/send packet */
  140. __u8 intervall; /* interrupt interval */
  141. struct sk_buff *skbuff; /* actual used buffer */
  142. struct urb *urb; /* transfer structure for usb routines */
  143. __u8 buffer[128]; /* buffer incoming/outgoing data */
  144. int bit_line; /* how much bits are in the fifo? */
  145. volatile __u8 usb_transfer_mode; /* switched between ISO and INT */
  146. iso_urb_struct iso[2]; /* need two urbs to have one always for pending */
  147. struct hisax_if *hif; /* hisax interface */
  148. int delete_flg; /* only delete skbuff once */
  149. int last_urblen; /* remember length of last packet */
  150. } usb_fifo;
  151. /* structure holding all data for one device */
  152. typedef struct hfcusb_data {
  153. /* HiSax Interface for loadable Layer1 drivers */
  154. struct hisax_d_if d_if; /* see hisax_if.h */
  155. struct hisax_b_if b_if[2]; /* see hisax_if.h */
  156. int protocol;
  157. struct usb_device *dev; /* our device */
  158. int if_used; /* used interface number */
  159. int alt_used; /* used alternate config */
  160. int ctrl_paksize; /* control pipe packet size */
  161. int ctrl_in_pipe, /* handles for control pipe */
  162. ctrl_out_pipe;
  163. int cfg_used; /* configuration index used */
  164. int vend_idx; /* vendor found */
  165. int b_mode[2]; /* B-channel mode */
  166. int l1_activated; /* layer 1 activated */
  167. int disc_flag; /* TRUE if device was disonnected to avoid some USB actions */
  168. int packet_size, iso_packet_size;
  169. /* control pipe background handling */
  170. ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE]; /* buffer holding queued data */
  171. volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt; /* input/output pointer + count */
  172. struct urb *ctrl_urb; /* transfer structure for control channel */
  173. struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
  174. struct usb_ctrlrequest ctrl_read; /* same for read request */
  175. __u8 old_led_state, led_state, led_new_data, led_b_active;
  176. volatile __u8 threshold_mask; /* threshold actually reported */
  177. volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
  178. usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
  179. volatile __u8 l1_state; /* actual l1 state */
  180. struct timer_list t3_timer; /* timer 3 for activation/deactivation */
  181. struct timer_list t4_timer; /* timer 4 for activation/deactivation */
  182. } hfcusb_data;
  183. static void collect_rx_frame(usb_fifo * fifo, __u8 * data, int len,
  184. int finish);
  185. static inline const char *
  186. symbolic(struct hfcusb_symbolic_list list[], const int num)
  187. {
  188. int i;
  189. for (i = 0; list[i].name != NULL; i++)
  190. if (list[i].num == num)
  191. return (list[i].name);
  192. return "<unknown ERROR>";
  193. }
  194. static void
  195. ctrl_start_transfer(hfcusb_data * hfc)
  196. {
  197. if (hfc->ctrl_cnt) {
  198. hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
  199. hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
  200. hfc->ctrl_urb->transfer_buffer = NULL;
  201. hfc->ctrl_urb->transfer_buffer_length = 0;
  202. hfc->ctrl_write.wIndex =
  203. cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
  204. hfc->ctrl_write.wValue =
  205. cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
  206. usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
  207. }
  208. } /* ctrl_start_transfer */
  209. static int
  210. queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val, int action)
  211. {
  212. ctrl_buft *buf;
  213. if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
  214. return (1); /* no space left */
  215. buf = &hfc->ctrl_buff[hfc->ctrl_in_idx]; /* pointer to new index */
  216. buf->hfc_reg = reg;
  217. buf->reg_val = val;
  218. buf->action = action;
  219. if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
  220. hfc->ctrl_in_idx = 0; /* pointer wrap */
  221. if (++hfc->ctrl_cnt == 1)
  222. ctrl_start_transfer(hfc);
  223. return (0);
  224. }
  225. static void
  226. ctrl_complete(struct urb *urb)
  227. {
  228. hfcusb_data *hfc = (hfcusb_data *) urb->context;
  229. ctrl_buft *buf;
  230. urb->dev = hfc->dev;
  231. if (hfc->ctrl_cnt) {
  232. buf = &hfc->ctrl_buff[hfc->ctrl_out_idx];
  233. hfc->ctrl_cnt--; /* decrement actual count */
  234. if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
  235. hfc->ctrl_out_idx = 0; /* pointer wrap */
  236. ctrl_start_transfer(hfc); /* start next transfer */
  237. }
  238. } /* ctrl_complete */
  239. /* write led data to auxport & invert if necessary */
  240. static void
  241. write_led(hfcusb_data * hfc, __u8 led_state)
  242. {
  243. if (led_state != hfc->old_led_state) {
  244. hfc->old_led_state = led_state;
  245. queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
  246. }
  247. }
  248. static void
  249. set_led_bit(hfcusb_data * hfc, signed short led_bits, int unset)
  250. {
  251. if (unset) {
  252. if (led_bits < 0)
  253. hfc->led_state |= abs(led_bits);
  254. else
  255. hfc->led_state &= ~led_bits;
  256. } else {
  257. if (led_bits < 0)
  258. hfc->led_state &= ~abs(led_bits);
  259. else
  260. hfc->led_state |= led_bits;
  261. }
  262. }
  263. /* handle LED requests */
  264. static void
  265. handle_led(hfcusb_data * hfc, int event)
  266. {
  267. hfcsusb_vdata *driver_info =
  268. (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
  269. /* if no scheme -> no LED action */
  270. if (driver_info->led_scheme == LED_OFF)
  271. return;
  272. switch (event) {
  273. case LED_POWER_ON:
  274. set_led_bit(hfc, driver_info->led_bits[0], 0);
  275. set_led_bit(hfc, driver_info->led_bits[1], 1);
  276. set_led_bit(hfc, driver_info->led_bits[2], 1);
  277. set_led_bit(hfc, driver_info->led_bits[3], 1);
  278. break;
  279. case LED_POWER_OFF:
  280. set_led_bit(hfc, driver_info->led_bits[0], 1);
  281. set_led_bit(hfc, driver_info->led_bits[1], 1);
  282. set_led_bit(hfc, driver_info->led_bits[2], 1);
  283. set_led_bit(hfc, driver_info->led_bits[3], 1);
  284. break;
  285. case LED_S0_ON:
  286. set_led_bit(hfc, driver_info->led_bits[1], 0);
  287. break;
  288. case LED_S0_OFF:
  289. set_led_bit(hfc, driver_info->led_bits[1], 1);
  290. break;
  291. case LED_B1_ON:
  292. set_led_bit(hfc, driver_info->led_bits[2], 0);
  293. break;
  294. case LED_B1_OFF:
  295. set_led_bit(hfc, driver_info->led_bits[2], 1);
  296. break;
  297. case LED_B2_ON:
  298. set_led_bit(hfc, driver_info->led_bits[3], 0);
  299. break;
  300. case LED_B2_OFF:
  301. set_led_bit(hfc, driver_info->led_bits[3], 1);
  302. break;
  303. }
  304. write_led(hfc, hfc->led_state);
  305. }
  306. /* ISDN l1 timer T3 expires */
  307. static void
  308. l1_timer_expire_t3(hfcusb_data * hfc)
  309. {
  310. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
  311. NULL);
  312. DBG(HFCUSB_DBG_STATES,
  313. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
  314. hfc->l1_activated = 0;
  315. handle_led(hfc, LED_S0_OFF);
  316. /* deactivate : */
  317. queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
  318. queue_control_request(hfc, HFCUSB_STATES, 3, 1);
  319. }
  320. /* ISDN l1 timer T4 expires */
  321. static void
  322. l1_timer_expire_t4(hfcusb_data * hfc)
  323. {
  324. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
  325. NULL);
  326. DBG(HFCUSB_DBG_STATES,
  327. "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
  328. hfc->l1_activated = 0;
  329. handle_led(hfc, LED_S0_OFF);
  330. }
  331. /* S0 state changed */
  332. static void
  333. s0_state_handler(hfcusb_data * hfc, __u8 state)
  334. {
  335. __u8 old_state;
  336. old_state = hfc->l1_state;
  337. if (state == old_state || state < 1 || state > 8)
  338. return;
  339. DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
  340. old_state, state);
  341. if (state < 4 || state == 7 || state == 8) {
  342. if (timer_pending(&hfc->t3_timer))
  343. del_timer(&hfc->t3_timer);
  344. DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
  345. }
  346. if (state >= 7) {
  347. if (timer_pending(&hfc->t4_timer))
  348. del_timer(&hfc->t4_timer);
  349. DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
  350. }
  351. if (state == 7 && !hfc->l1_activated) {
  352. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
  353. PH_ACTIVATE | INDICATION, NULL);
  354. DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
  355. hfc->l1_activated = 1;
  356. handle_led(hfc, LED_S0_ON);
  357. } else if (state <= 3 /* && activated */ ) {
  358. if (old_state == 7 || old_state == 8) {
  359. DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
  360. if (!timer_pending(&hfc->t4_timer)) {
  361. hfc->t4_timer.expires =
  362. jiffies + (HFC_TIMER_T4 * HZ) / 1000;
  363. add_timer(&hfc->t4_timer);
  364. }
  365. } else {
  366. hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
  367. PH_DEACTIVATE | INDICATION,
  368. NULL);
  369. DBG(HFCUSB_DBG_STATES,
  370. "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
  371. hfc->l1_activated = 0;
  372. handle_led(hfc, LED_S0_OFF);
  373. }
  374. }
  375. hfc->l1_state = state;
  376. }
  377. static void
  378. fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
  379. void *buf, int num_packets, int packet_size, int interval,
  380. usb_complete_t complete, void *context)
  381. {
  382. int k;
  383. urb->dev = dev;
  384. urb->pipe = pipe;
  385. urb->complete = complete;
  386. urb->number_of_packets = num_packets;
  387. urb->transfer_buffer_length = packet_size * num_packets;
  388. urb->context = context;
  389. urb->transfer_buffer = buf;
  390. urb->transfer_flags = URB_ISO_ASAP;
  391. urb->actual_length = 0;
  392. urb->interval = interval;
  393. for (k = 0; k < num_packets; k++) {
  394. urb->iso_frame_desc[k].offset = packet_size * k;
  395. urb->iso_frame_desc[k].length = packet_size;
  396. urb->iso_frame_desc[k].actual_length = 0;
  397. }
  398. }
  399. /* allocs urbs and start isoc transfer with two pending urbs to avoid
  400. * gaps in the transfer chain
  401. */
  402. static int
  403. start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,
  404. usb_complete_t complete, int packet_size)
  405. {
  406. int i, k, errcode;
  407. DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
  408. fifo->fifonum);
  409. /* allocate Memory for Iso out Urbs */
  410. for (i = 0; i < 2; i++) {
  411. if (!(fifo->iso[i].purb)) {
  412. fifo->iso[i].purb =
  413. usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
  414. if (!(fifo->iso[i].purb)) {
  415. printk(KERN_INFO
  416. "alloc urb for fifo %i failed!!!",
  417. fifo->fifonum);
  418. }
  419. fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
  420. /* Init the first iso */
  421. if (ISO_BUFFER_SIZE >=
  422. (fifo->usb_packet_maxlen *
  423. num_packets_per_urb)) {
  424. fill_isoc_urb(fifo->iso[i].purb,
  425. fifo->hfc->dev, fifo->pipe,
  426. fifo->iso[i].buffer,
  427. num_packets_per_urb,
  428. fifo->usb_packet_maxlen,
  429. fifo->intervall, complete,
  430. &fifo->iso[i]);
  431. memset(fifo->iso[i].buffer, 0,
  432. sizeof(fifo->iso[i].buffer));
  433. /* defining packet delimeters in fifo->buffer */
  434. for (k = 0; k < num_packets_per_urb; k++) {
  435. fifo->iso[i].purb->
  436. iso_frame_desc[k].offset =
  437. k * packet_size;
  438. fifo->iso[i].purb->
  439. iso_frame_desc[k].length =
  440. packet_size;
  441. }
  442. } else {
  443. printk(KERN_INFO
  444. "HFC-S USB: ISO Buffer size to small!\n");
  445. }
  446. }
  447. fifo->bit_line = BITLINE_INF;
  448. errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
  449. fifo->active = (errcode >= 0) ? 1 : 0;
  450. if (errcode < 0)
  451. printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
  452. i, errcode, symbolic(urb_errlist, errcode));
  453. }
  454. return (fifo->active);
  455. }
  456. /* stops running iso chain and frees their pending urbs */
  457. static void
  458. stop_isoc_chain(usb_fifo * fifo)
  459. {
  460. int i;
  461. for (i = 0; i < 2; i++) {
  462. if (fifo->iso[i].purb) {
  463. DBG(HFCUSB_DBG_INIT,
  464. "HFC-S USB: Stopping iso chain for fifo %i.%i",
  465. fifo->fifonum, i);
  466. usb_kill_urb(fifo->iso[i].purb);
  467. usb_free_urb(fifo->iso[i].purb);
  468. fifo->iso[i].purb = NULL;
  469. }
  470. }
  471. usb_kill_urb(fifo->urb);
  472. usb_free_urb(fifo->urb);
  473. fifo->urb = NULL;
  474. fifo->active = 0;
  475. }
  476. /* defines how much ISO packets are handled in one URB */
  477. static int iso_packets[8] =
  478. { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
  479. ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
  480. };
  481. static void
  482. tx_iso_complete(struct urb *urb)
  483. {
  484. iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
  485. usb_fifo *fifo = context_iso_urb->owner_fifo;
  486. hfcusb_data *hfc = fifo->hfc;
  487. int k, tx_offset, num_isoc_packets, sink, len, current_len,
  488. errcode;
  489. int frame_complete, transp_mode, fifon, status;
  490. __u8 threshbit;
  491. fifon = fifo->fifonum;
  492. status = urb->status;
  493. tx_offset = 0;
  494. /* ISO transfer only partially completed,
  495. look at individual frame status for details */
  496. if (status == -EXDEV) {
  497. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
  498. ", urb->status %d, fifonum %d\n",
  499. status, fifon);
  500. for (k = 0; k < iso_packets[fifon]; ++k) {
  501. errcode = urb->iso_frame_desc[k].status;
  502. if (errcode)
  503. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
  504. "packet %i, status: %i\n",
  505. k, errcode);
  506. }
  507. // clear status, so go on with ISO transfers
  508. status = 0;
  509. }
  510. if (fifo->active && !status) {
  511. transp_mode = 0;
  512. if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
  513. transp_mode = 1;
  514. /* is FifoFull-threshold set for our channel? */
  515. threshbit = (hfc->threshold_mask & (1 << fifon));
  516. num_isoc_packets = iso_packets[fifon];
  517. /* predict dataflow to avoid fifo overflow */
  518. if (fifon >= HFCUSB_D_TX) {
  519. sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
  520. } else {
  521. sink = (threshbit) ? SINK_MIN : SINK_MAX;
  522. }
  523. fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
  524. context_iso_urb->buffer, num_isoc_packets,
  525. fifo->usb_packet_maxlen, fifo->intervall,
  526. tx_iso_complete, urb->context);
  527. memset(context_iso_urb->buffer, 0,
  528. sizeof(context_iso_urb->buffer));
  529. frame_complete = 0;
  530. /* Generate next ISO Packets */
  531. for (k = 0; k < num_isoc_packets; ++k) {
  532. if (fifo->skbuff) {
  533. len = fifo->skbuff->len;
  534. /* we lower data margin every msec */
  535. fifo->bit_line -= sink;
  536. current_len = (0 - fifo->bit_line) / 8;
  537. /* maximum 15 byte for every ISO packet makes our life easier */
  538. if (current_len > 14)
  539. current_len = 14;
  540. current_len =
  541. (len <=
  542. current_len) ? len : current_len;
  543. /* how much bit do we put on the line? */
  544. fifo->bit_line += current_len * 8;
  545. context_iso_urb->buffer[tx_offset] = 0;
  546. if (current_len == len) {
  547. if (!transp_mode) {
  548. /* here frame completion */
  549. context_iso_urb->
  550. buffer[tx_offset] = 1;
  551. /* add 2 byte flags and 16bit CRC at end of ISDN frame */
  552. fifo->bit_line += 32;
  553. }
  554. frame_complete = 1;
  555. }
  556. memcpy(context_iso_urb->buffer +
  557. tx_offset + 1, fifo->skbuff->data,
  558. current_len);
  559. skb_pull(fifo->skbuff, current_len);
  560. /* define packet delimeters within the URB buffer */
  561. urb->iso_frame_desc[k].offset = tx_offset;
  562. urb->iso_frame_desc[k].length =
  563. current_len + 1;
  564. tx_offset += (current_len + 1);
  565. } else {
  566. urb->iso_frame_desc[k].offset =
  567. tx_offset++;
  568. urb->iso_frame_desc[k].length = 1;
  569. fifo->bit_line -= sink; /* we lower data margin every msec */
  570. if (fifo->bit_line < BITLINE_INF) {
  571. fifo->bit_line = BITLINE_INF;
  572. }
  573. }
  574. if (frame_complete) {
  575. fifo->delete_flg = 1;
  576. fifo->hif->l1l2(fifo->hif,
  577. PH_DATA | CONFIRM,
  578. (void *) (unsigned long) fifo->skbuff->
  579. truesize);
  580. if (fifo->skbuff && fifo->delete_flg) {
  581. dev_kfree_skb_any(fifo->skbuff);
  582. fifo->skbuff = NULL;
  583. fifo->delete_flg = 0;
  584. }
  585. frame_complete = 0;
  586. }
  587. }
  588. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  589. if (errcode < 0) {
  590. printk(KERN_INFO
  591. "HFC-S USB: error submitting ISO URB: %d\n",
  592. errcode);
  593. }
  594. } else {
  595. if (status && !hfc->disc_flag) {
  596. printk(KERN_INFO
  597. "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
  598. status, symbolic(urb_errlist, status), fifon);
  599. }
  600. }
  601. }
  602. static void
  603. rx_iso_complete(struct urb *urb)
  604. {
  605. iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
  606. usb_fifo *fifo = context_iso_urb->owner_fifo;
  607. hfcusb_data *hfc = fifo->hfc;
  608. int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
  609. status;
  610. unsigned int iso_status;
  611. __u8 *buf;
  612. static __u8 eof[8];
  613. fifon = fifo->fifonum;
  614. status = urb->status;
  615. if (urb->status == -EOVERFLOW) {
  616. DBG(HFCUSB_DBG_VERBOSE_USB,
  617. "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
  618. status = 0;
  619. }
  620. /* ISO transfer only partially completed,
  621. look at individual frame status for details */
  622. if (status == -EXDEV) {
  623. DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
  624. "urb->status %d, fifonum %d\n",
  625. status, fifon);
  626. status = 0;
  627. }
  628. if (fifo->active && !status) {
  629. num_isoc_packets = iso_packets[fifon];
  630. maxlen = fifo->usb_packet_maxlen;
  631. for (k = 0; k < num_isoc_packets; ++k) {
  632. len = urb->iso_frame_desc[k].actual_length;
  633. offset = urb->iso_frame_desc[k].offset;
  634. buf = context_iso_urb->buffer + offset;
  635. iso_status = urb->iso_frame_desc[k].status;
  636. if (iso_status && !hfc->disc_flag)
  637. DBG(HFCUSB_DBG_VERBOSE_USB,
  638. "HFC-S USB: rx_iso_complete "
  639. "ISO packet %i, status: %i\n",
  640. k, iso_status);
  641. if (fifon == HFCUSB_D_RX) {
  642. DBG(HFCUSB_DBG_VERBOSE_USB,
  643. "HFC-S USB: ISO-D-RX lst_urblen:%2d "
  644. "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
  645. fifo->last_urblen, len, maxlen,
  646. eof[5]);
  647. DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
  648. }
  649. if (fifo->last_urblen != maxlen) {
  650. /* the threshold mask is in the 2nd status byte */
  651. hfc->threshold_mask = buf[1];
  652. /* care for L1 state only for D-Channel
  653. to avoid overlapped iso completions */
  654. if (fifon == HFCUSB_D_RX) {
  655. /* the S0 state is in the upper half
  656. of the 1st status byte */
  657. s0_state_handler(hfc, buf[0] >> 4);
  658. }
  659. eof[fifon] = buf[0] & 1;
  660. if (len > 2)
  661. collect_rx_frame(fifo, buf + 2,
  662. len - 2,
  663. (len < maxlen) ?
  664. eof[fifon] : 0);
  665. } else {
  666. collect_rx_frame(fifo, buf, len,
  667. (len <
  668. maxlen) ? eof[fifon] :
  669. 0);
  670. }
  671. fifo->last_urblen = len;
  672. }
  673. fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
  674. context_iso_urb->buffer, num_isoc_packets,
  675. fifo->usb_packet_maxlen, fifo->intervall,
  676. rx_iso_complete, urb->context);
  677. errcode = usb_submit_urb(urb, GFP_ATOMIC);
  678. if (errcode < 0) {
  679. printk(KERN_ERR
  680. "HFC-S USB: error submitting ISO URB: %d\n",
  681. errcode);
  682. }
  683. } else {
  684. if (status && !hfc->disc_flag) {
  685. printk(KERN_ERR
  686. "HFC-S USB: rx_iso_complete : "
  687. "urb->status %d, fifonum %d\n",
  688. status, fifon);
  689. }
  690. }
  691. }
  692. /* collect rx data from INT- and ISO-URBs */
  693. static void
  694. collect_rx_frame(usb_fifo * fifo, __u8 * data, int len, int finish)
  695. {
  696. hfcusb_data *hfc = fifo->hfc;
  697. int transp_mode, fifon;
  698. fifon = fifo->fifonum;
  699. transp_mode = 0;
  700. if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
  701. transp_mode = 1;
  702. if (!fifo->skbuff) {
  703. fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
  704. if (!fifo->skbuff) {
  705. printk(KERN_ERR
  706. "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
  707. fifon);
  708. return;
  709. }
  710. }
  711. if (len) {
  712. if (fifo->skbuff->len + len < fifo->max_size) {
  713. memcpy(skb_put(fifo->skbuff, len), data, len);
  714. } else {
  715. DBG(HFCUSB_DBG_FIFO_ERR,
  716. "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
  717. fifo->max_size, fifon);
  718. DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
  719. skb_trim(fifo->skbuff, 0);
  720. }
  721. }
  722. if (transp_mode && fifo->skbuff->len >= 128) {
  723. fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
  724. fifo->skbuff);
  725. fifo->skbuff = NULL;
  726. return;
  727. }
  728. /* we have a complete hdlc packet */
  729. if (finish) {
  730. if ((!fifo->skbuff->data[fifo->skbuff->len - 1])
  731. && (fifo->skbuff->len > 3)) {
  732. if (fifon == HFCUSB_D_RX) {
  733. DBG(HFCUSB_DBG_DCHANNEL,
  734. "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
  735. DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
  736. }
  737. /* remove CRC & status */
  738. skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
  739. if (fifon == HFCUSB_PCM_RX) {
  740. fifo->hif->l1l2(fifo->hif,
  741. PH_DATA_E | INDICATION,
  742. fifo->skbuff);
  743. } else
  744. fifo->hif->l1l2(fifo->hif,
  745. PH_DATA | INDICATION,
  746. fifo->skbuff);
  747. fifo->skbuff = NULL; /* buffer was freed from upper layer */
  748. } else {
  749. DBG(HFCUSB_DBG_FIFO_ERR,
  750. "HFC-S USB: ERROR frame len(%d) fifo(%d)",
  751. fifo->skbuff->len, fifon);
  752. DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
  753. skb_trim(fifo->skbuff, 0);
  754. }
  755. }
  756. }
  757. static void
  758. rx_int_complete(struct urb *urb)
  759. {
  760. int len;
  761. int status;
  762. __u8 *buf, maxlen, fifon;
  763. usb_fifo *fifo = (usb_fifo *) urb->context;
  764. hfcusb_data *hfc = fifo->hfc;
  765. static __u8 eof[8];
  766. urb->dev = hfc->dev; /* security init */
  767. fifon = fifo->fifonum;
  768. if ((!fifo->active) || (urb->status)) {
  769. DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
  770. fifon, urb->status);
  771. fifo->urb->interval = 0; /* cancel automatic rescheduling */
  772. if (fifo->skbuff) {
  773. dev_kfree_skb_any(fifo->skbuff);
  774. fifo->skbuff = NULL;
  775. }
  776. return;
  777. }
  778. len = urb->actual_length;
  779. buf = fifo->buffer;
  780. maxlen = fifo->usb_packet_maxlen;
  781. if (fifon == HFCUSB_D_RX) {
  782. DBG(HFCUSB_DBG_VERBOSE_USB,
  783. "HFC-S USB: INT-D-RX lst_urblen:%2d "
  784. "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
  785. fifo->last_urblen, len, maxlen,
  786. eof[5]);
  787. DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
  788. }
  789. if (fifo->last_urblen != fifo->usb_packet_maxlen) {
  790. /* the threshold mask is in the 2nd status byte */
  791. hfc->threshold_mask = buf[1];
  792. /* the S0 state is in the upper half of the 1st status byte */
  793. s0_state_handler(hfc, buf[0] >> 4);
  794. eof[fifon] = buf[0] & 1;
  795. /* if we have more than the 2 status bytes -> collect data */
  796. if (len > 2)
  797. collect_rx_frame(fifo, buf + 2,
  798. urb->actual_length - 2,
  799. (len < maxlen) ? eof[fifon] : 0);
  800. } else {
  801. collect_rx_frame(fifo, buf, urb->actual_length,
  802. (len < maxlen) ? eof[fifon] : 0);
  803. }
  804. fifo->last_urblen = urb->actual_length;
  805. status = usb_submit_urb(urb, GFP_ATOMIC);
  806. if (status) {
  807. printk(KERN_INFO
  808. "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
  809. __FUNCTION__, fifon);
  810. }
  811. }
  812. /* start initial INT-URB for certain fifo */
  813. static void
  814. start_int_fifo(usb_fifo * fifo)
  815. {
  816. int errcode;
  817. DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
  818. fifo->fifonum);
  819. if (!fifo->urb) {
  820. fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
  821. if (!fifo->urb)
  822. return;
  823. }
  824. usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
  825. fifo->buffer, fifo->usb_packet_maxlen,
  826. rx_int_complete, fifo, fifo->intervall);
  827. fifo->active = 1; /* must be marked active */
  828. errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
  829. if (errcode) {
  830. printk(KERN_ERR
  831. "HFC-S USB: submit URB error(start_int_info): status:%i\n",
  832. 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: unkown 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, err;
  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. err = 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->led_new_data = 0;
  1044. hfc->old_led_state = 0;
  1045. /* init the t3 timer */
  1046. init_timer(&hfc->t3_timer);
  1047. hfc->t3_timer.data = (long) hfc;
  1048. hfc->t3_timer.function = (void *) l1_timer_expire_t3;
  1049. /* init the t4 timer */
  1050. init_timer(&hfc->t4_timer);
  1051. hfc->t4_timer.data = (long) hfc;
  1052. hfc->t4_timer.function = (void *) l1_timer_expire_t4;
  1053. /* init the background machinery for control requests */
  1054. hfc->ctrl_read.bRequestType = 0xc0;
  1055. hfc->ctrl_read.bRequest = 1;
  1056. hfc->ctrl_read.wLength = cpu_to_le16(1);
  1057. hfc->ctrl_write.bRequestType = 0x40;
  1058. hfc->ctrl_write.bRequest = 0;
  1059. hfc->ctrl_write.wLength = 0;
  1060. usb_fill_control_urb(hfc->ctrl_urb,
  1061. hfc->dev,
  1062. hfc->ctrl_out_pipe,
  1063. (u_char *) & hfc->ctrl_write,
  1064. NULL, 0, ctrl_complete, hfc);
  1065. /* Init All Fifos */
  1066. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1067. hfc->fifos[i].iso[0].purb = NULL;
  1068. hfc->fifos[i].iso[1].purb = NULL;
  1069. hfc->fifos[i].active = 0;
  1070. }
  1071. /* register Modul to upper Hisax Layers */
  1072. hfc->d_if.owner = THIS_MODULE;
  1073. hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
  1074. hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
  1075. for (i = 0; i < 2; i++) {
  1076. hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
  1077. hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
  1078. p_b_if[i] = &hfc->b_if[i];
  1079. }
  1080. /* default Prot: EURO ISDN, should be a module_param */
  1081. hfc->protocol = 2;
  1082. i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
  1083. if (i) {
  1084. printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
  1085. return i;
  1086. }
  1087. #ifdef CONFIG_HISAX_DEBUG
  1088. hfc_debug = debug;
  1089. #endif
  1090. for (i = 0; i < 4; i++)
  1091. hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
  1092. for (i = 4; i < 8; i++)
  1093. hfc->fifos[i].hif = &hfc->d_if.ifc;
  1094. /* 3 (+1) INT IN + 3 ISO OUT */
  1095. if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
  1096. start_int_fifo(hfc->fifos + HFCUSB_D_RX);
  1097. if (hfc->fifos[HFCUSB_PCM_RX].pipe)
  1098. start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
  1099. start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
  1100. start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
  1101. }
  1102. /* 3 (+1) ISO IN + 3 ISO OUT */
  1103. if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
  1104. start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
  1105. rx_iso_complete, 16);
  1106. if (hfc->fifos[HFCUSB_PCM_RX].pipe)
  1107. start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
  1108. ISOC_PACKETS_D, rx_iso_complete,
  1109. 16);
  1110. start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
  1111. rx_iso_complete, 16);
  1112. start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
  1113. rx_iso_complete, 16);
  1114. }
  1115. start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
  1116. tx_iso_complete, 1);
  1117. start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
  1118. tx_iso_complete, 1);
  1119. start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
  1120. tx_iso_complete, 1);
  1121. handle_led(hfc, LED_POWER_ON);
  1122. return (0);
  1123. }
  1124. /* initial callback for each plugged USB device */
  1125. static int
  1126. hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1127. {
  1128. struct usb_device *dev = interface_to_usbdev(intf);
  1129. hfcusb_data *context;
  1130. struct usb_host_interface *iface = intf->cur_altsetting;
  1131. struct usb_host_interface *iface_used = NULL;
  1132. struct usb_host_endpoint *ep;
  1133. int ifnum = iface->desc.bInterfaceNumber;
  1134. int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
  1135. attr, cfg_found, cidx, ep_addr;
  1136. int cmptbl[16], small_match, iso_packet_size, packet_size,
  1137. alt_used = 0;
  1138. hfcsusb_vdata *driver_info;
  1139. vend_idx = 0xffff;
  1140. for (i = 0; hfcusb_idtab[i].idVendor; i++) {
  1141. if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
  1142. && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
  1143. vend_idx = i;
  1144. continue;
  1145. }
  1146. }
  1147. printk(KERN_INFO
  1148. "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
  1149. ifnum, iface->desc.bAlternateSetting, intf->minor);
  1150. if (vend_idx != 0xffff) {
  1151. /* if vendor and product ID is OK, start probing alternate settings */
  1152. alt_idx = 0;
  1153. small_match = 0xffff;
  1154. /* default settings */
  1155. iso_packet_size = 16;
  1156. packet_size = 64;
  1157. while (alt_idx < intf->num_altsetting) {
  1158. iface = intf->altsetting + alt_idx;
  1159. probe_alt_setting = iface->desc.bAlternateSetting;
  1160. cfg_used = 0;
  1161. /* check for config EOL element */
  1162. while (validconf[cfg_used][0]) {
  1163. cfg_found = 1;
  1164. vcf = validconf[cfg_used];
  1165. /* first endpoint descriptor */
  1166. ep = iface->endpoint;
  1167. memcpy(cmptbl, vcf, 16 * sizeof(int));
  1168. /* check for all endpoints in this alternate setting */
  1169. for (i = 0; i < iface->desc.bNumEndpoints;
  1170. i++) {
  1171. ep_addr =
  1172. ep->desc.bEndpointAddress;
  1173. /* get endpoint base */
  1174. idx = ((ep_addr & 0x7f) - 1) * 2;
  1175. if (ep_addr & 0x80)
  1176. idx++;
  1177. attr = ep->desc.bmAttributes;
  1178. if (cmptbl[idx] == EP_NUL) {
  1179. cfg_found = 0;
  1180. }
  1181. if (attr == USB_ENDPOINT_XFER_INT
  1182. && cmptbl[idx] == EP_INT)
  1183. cmptbl[idx] = EP_NUL;
  1184. if (attr == USB_ENDPOINT_XFER_BULK
  1185. && cmptbl[idx] == EP_BLK)
  1186. cmptbl[idx] = EP_NUL;
  1187. if (attr == USB_ENDPOINT_XFER_ISOC
  1188. && cmptbl[idx] == EP_ISO)
  1189. cmptbl[idx] = EP_NUL;
  1190. /* check if all INT endpoints match minimum interval */
  1191. if ((attr == USB_ENDPOINT_XFER_INT)
  1192. && (ep->desc.bInterval < vcf[17])) {
  1193. cfg_found = 0;
  1194. }
  1195. ep++;
  1196. }
  1197. for (i = 0; i < 16; i++) {
  1198. /* all entries must be EP_NOP or EP_NUL for a valid config */
  1199. if (cmptbl[i] != EP_NOP
  1200. && cmptbl[i] != EP_NUL)
  1201. cfg_found = 0;
  1202. }
  1203. if (cfg_found) {
  1204. if (cfg_used < small_match) {
  1205. small_match = cfg_used;
  1206. alt_used =
  1207. probe_alt_setting;
  1208. iface_used = iface;
  1209. }
  1210. }
  1211. cfg_used++;
  1212. }
  1213. alt_idx++;
  1214. } /* (alt_idx < intf->num_altsetting) */
  1215. /* found a valid USB Ta Endpint config */
  1216. if (small_match != 0xffff) {
  1217. iface = iface_used;
  1218. if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
  1219. return (-ENOMEM); /* got no mem */
  1220. ep = iface->endpoint;
  1221. vcf = validconf[small_match];
  1222. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1223. ep_addr = ep->desc.bEndpointAddress;
  1224. /* get endpoint base */
  1225. idx = ((ep_addr & 0x7f) - 1) * 2;
  1226. if (ep_addr & 0x80)
  1227. idx++;
  1228. cidx = idx & 7;
  1229. attr = ep->desc.bmAttributes;
  1230. /* init Endpoints */
  1231. if (vcf[idx] != EP_NOP
  1232. && vcf[idx] != EP_NUL) {
  1233. switch (attr) {
  1234. case USB_ENDPOINT_XFER_INT:
  1235. context->
  1236. fifos[cidx].
  1237. pipe =
  1238. usb_rcvintpipe
  1239. (dev,
  1240. ep->desc.
  1241. bEndpointAddress);
  1242. context->
  1243. fifos[cidx].
  1244. usb_transfer_mode
  1245. = USB_INT;
  1246. packet_size =
  1247. le16_to_cpu(ep->desc.wMaxPacketSize);
  1248. break;
  1249. case USB_ENDPOINT_XFER_BULK:
  1250. if (ep_addr & 0x80)
  1251. context->
  1252. fifos
  1253. [cidx].
  1254. pipe =
  1255. usb_rcvbulkpipe
  1256. (dev,
  1257. ep->
  1258. desc.
  1259. bEndpointAddress);
  1260. else
  1261. context->
  1262. fifos
  1263. [cidx].
  1264. pipe =
  1265. usb_sndbulkpipe
  1266. (dev,
  1267. ep->
  1268. desc.
  1269. bEndpointAddress);
  1270. context->
  1271. fifos[cidx].
  1272. usb_transfer_mode
  1273. = USB_BULK;
  1274. packet_size =
  1275. le16_to_cpu(ep->desc.wMaxPacketSize);
  1276. break;
  1277. case USB_ENDPOINT_XFER_ISOC:
  1278. if (ep_addr & 0x80)
  1279. context->
  1280. fifos
  1281. [cidx].
  1282. pipe =
  1283. usb_rcvisocpipe
  1284. (dev,
  1285. ep->
  1286. desc.
  1287. bEndpointAddress);
  1288. else
  1289. context->
  1290. fifos
  1291. [cidx].
  1292. pipe =
  1293. usb_sndisocpipe
  1294. (dev,
  1295. ep->
  1296. desc.
  1297. bEndpointAddress);
  1298. context->
  1299. fifos[cidx].
  1300. usb_transfer_mode
  1301. = USB_ISOC;
  1302. iso_packet_size =
  1303. le16_to_cpu(ep->desc.wMaxPacketSize);
  1304. break;
  1305. default:
  1306. context->
  1307. fifos[cidx].
  1308. pipe = 0;
  1309. } /* switch attribute */
  1310. if (context->fifos[cidx].pipe) {
  1311. context->fifos[cidx].
  1312. fifonum = cidx;
  1313. context->fifos[cidx].hfc =
  1314. context;
  1315. context->fifos[cidx].usb_packet_maxlen =
  1316. le16_to_cpu(ep->desc.wMaxPacketSize);
  1317. context->fifos[cidx].
  1318. intervall =
  1319. ep->desc.bInterval;
  1320. context->fifos[cidx].
  1321. skbuff = NULL;
  1322. }
  1323. }
  1324. ep++;
  1325. }
  1326. context->dev = dev; /* save device */
  1327. context->if_used = ifnum; /* save used interface */
  1328. context->alt_used = alt_used; /* and alternate config */
  1329. context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
  1330. context->cfg_used = vcf[16]; /* store used config */
  1331. context->vend_idx = vend_idx; /* store found vendor */
  1332. context->packet_size = packet_size;
  1333. context->iso_packet_size = iso_packet_size;
  1334. /* create the control pipes needed for register access */
  1335. context->ctrl_in_pipe =
  1336. usb_rcvctrlpipe(context->dev, 0);
  1337. context->ctrl_out_pipe =
  1338. usb_sndctrlpipe(context->dev, 0);
  1339. context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
  1340. driver_info =
  1341. (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
  1342. driver_info;
  1343. printk(KERN_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
  1370. *intf)
  1371. {
  1372. hfcusb_data *context = usb_get_intfdata(intf);
  1373. int i;
  1374. handle_led(context, LED_POWER_OFF);
  1375. schedule_timeout((10 * HZ) / 1000);
  1376. printk(KERN_INFO "HFC-S USB: device disconnect\n");
  1377. context->disc_flag = 1;
  1378. usb_set_intfdata(intf, NULL);
  1379. if (!context)
  1380. return;
  1381. if (timer_pending(&context->t3_timer))
  1382. del_timer(&context->t3_timer);
  1383. if (timer_pending(&context->t4_timer))
  1384. del_timer(&context->t4_timer);
  1385. /* tell all fifos to terminate */
  1386. for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
  1387. if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
  1388. if (context->fifos[i].active > 0) {
  1389. stop_isoc_chain(&context->fifos[i]);
  1390. DBG(HFCUSB_DBG_INIT,
  1391. "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
  1392. __FUNCTION__, i);
  1393. }
  1394. } else {
  1395. if (context->fifos[i].active > 0) {
  1396. context->fifos[i].active = 0;
  1397. DBG(HFCUSB_DBG_INIT,
  1398. "HFC-S USB: %s unlinking URB for Fifo(%i)",
  1399. __FUNCTION__, i);
  1400. }
  1401. usb_kill_urb(context->fifos[i].urb);
  1402. usb_free_urb(context->fifos[i].urb);
  1403. context->fifos[i].urb = NULL;
  1404. }
  1405. context->fifos[i].active = 0;
  1406. }
  1407. usb_kill_urb(context->ctrl_urb);
  1408. usb_free_urb(context->ctrl_urb);
  1409. context->ctrl_urb = NULL;
  1410. hisax_unregister(&context->d_if);
  1411. kfree(context); /* free our structure again */
  1412. }
  1413. static struct usb_driver hfc_drv = {
  1414. .name = "hfc_usb",
  1415. .id_table = hfcusb_idtab,
  1416. .probe = hfc_usb_probe,
  1417. .disconnect = hfc_usb_disconnect,
  1418. };
  1419. static void __exit
  1420. hfc_usb_mod_exit(void)
  1421. {
  1422. usb_deregister(&hfc_drv); /* release our driver */
  1423. printk(KERN_INFO "HFC-S USB: module removed\n");
  1424. }
  1425. static int __init
  1426. hfc_usb_mod_init(void)
  1427. {
  1428. char revstr[30], datestr[30], dummy[30];
  1429. #ifndef CONFIG_HISAX_DEBUG
  1430. hfc_debug = debug;
  1431. #endif
  1432. sscanf(hfcusb_revision,
  1433. "%s %s $ %s %s %s $ ", dummy, revstr,
  1434. dummy, datestr, dummy);
  1435. printk(KERN_INFO
  1436. "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
  1437. revstr, datestr, debug);
  1438. if (usb_register(&hfc_drv)) {
  1439. printk(KERN_INFO
  1440. "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
  1441. return (-1); /* unable to register */
  1442. }
  1443. return (0);
  1444. }
  1445. module_init(hfc_usb_mod_init);
  1446. module_exit(hfc_usb_mod_exit);
  1447. MODULE_AUTHOR(DRIVER_AUTHOR);
  1448. MODULE_DESCRIPTION(DRIVER_DESC);
  1449. MODULE_LICENSE("GPL");
  1450. MODULE_DEVICE_TABLE(usb, hfcusb_idtab);