hfc_usb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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);