hfc_usb.c 44 KB

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