hfc_usb.c 44 KB

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