st5481_usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Driver for ST5481 USB ISDN modem
  3. *
  4. * Author Frode Isaksen
  5. * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
  6. * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/usb.h>
  14. #include <linux/slab.h>
  15. #include "st5481.h"
  16. static int st5481_isoc_flatten(struct urb *urb);
  17. /* ======================================================================
  18. * control pipe
  19. */
  20. /*
  21. * Send the next endpoint 0 request stored in the FIFO.
  22. * Called either by the completion or by usb_ctrl_msg.
  23. */
  24. static void usb_next_ctrl_msg(struct urb *urb,
  25. struct st5481_adapter *adapter)
  26. {
  27. struct st5481_ctrl *ctrl = &adapter->ctrl;
  28. int r_index;
  29. if (test_and_set_bit(0, &ctrl->busy)) {
  30. return;
  31. }
  32. if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
  33. test_and_clear_bit(0,&ctrl->busy);
  34. return;
  35. }
  36. urb->setup_packet =
  37. (unsigned char *)&ctrl->msg_fifo.data[r_index];
  38. DBG(1,"request=0x%02x,value=0x%04x,index=%x",
  39. ((struct ctrl_msg *)urb->setup_packet)->dr.bRequest,
  40. ((struct ctrl_msg *)urb->setup_packet)->dr.wValue,
  41. ((struct ctrl_msg *)urb->setup_packet)->dr.wIndex);
  42. // Prepare the URB
  43. urb->dev = adapter->usb_dev;
  44. SUBMIT_URB(urb, GFP_ATOMIC);
  45. }
  46. /*
  47. * Asynchronous endpoint 0 request (async version of usb_control_msg).
  48. * The request will be queued up in a FIFO if the endpoint is busy.
  49. */
  50. static void usb_ctrl_msg(struct st5481_adapter *adapter,
  51. u8 request, u8 requesttype, u16 value, u16 index,
  52. ctrl_complete_t complete, void *context)
  53. {
  54. struct st5481_ctrl *ctrl = &adapter->ctrl;
  55. int w_index;
  56. struct ctrl_msg *ctrl_msg;
  57. if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
  58. WARNING("control msg FIFO full");
  59. return;
  60. }
  61. ctrl_msg = &ctrl->msg_fifo.data[w_index];
  62. ctrl_msg->dr.bRequestType = requesttype;
  63. ctrl_msg->dr.bRequest = request;
  64. ctrl_msg->dr.wValue = cpu_to_le16p(&value);
  65. ctrl_msg->dr.wIndex = cpu_to_le16p(&index);
  66. ctrl_msg->dr.wLength = 0;
  67. ctrl_msg->complete = complete;
  68. ctrl_msg->context = context;
  69. usb_next_ctrl_msg(ctrl->urb, adapter);
  70. }
  71. /*
  72. * Asynchronous endpoint 0 device request.
  73. */
  74. void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
  75. u8 request, u16 value,
  76. ctrl_complete_t complete, void *context)
  77. {
  78. usb_ctrl_msg(adapter, request,
  79. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  80. value, 0, complete, context);
  81. }
  82. /*
  83. * Asynchronous pipe reset (async version of usb_clear_halt).
  84. */
  85. void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
  86. u_char pipe,
  87. ctrl_complete_t complete, void *context)
  88. {
  89. DBG(1,"pipe=%02x",pipe);
  90. usb_ctrl_msg(adapter,
  91. USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
  92. 0, pipe, complete, context);
  93. }
  94. /*
  95. Physical level functions
  96. */
  97. void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
  98. {
  99. DBG(8,"command=%s", ST5481_CMD_string(command));
  100. st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
  101. }
  102. /*
  103. * The request on endpoint 0 has completed.
  104. * Call the user provided completion routine and try
  105. * to send the next request.
  106. */
  107. static void usb_ctrl_complete(struct urb *urb)
  108. {
  109. struct st5481_adapter *adapter = urb->context;
  110. struct st5481_ctrl *ctrl = &adapter->ctrl;
  111. struct ctrl_msg *ctrl_msg;
  112. if (unlikely(urb->status < 0)) {
  113. switch (urb->status) {
  114. case -ENOENT:
  115. case -ESHUTDOWN:
  116. case -ECONNRESET:
  117. DBG(1,"urb killed status %d", urb->status);
  118. return; // Give up
  119. default:
  120. WARNING("urb status %d",urb->status);
  121. break;
  122. }
  123. }
  124. ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
  125. if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) {
  126. /* Special case handling for pipe reset */
  127. le16_to_cpus(&ctrl_msg->dr.wIndex);
  128. usb_reset_endpoint(adapter->usb_dev, ctrl_msg->dr.wIndex);
  129. }
  130. if (ctrl_msg->complete)
  131. ctrl_msg->complete(ctrl_msg->context);
  132. clear_bit(0, &ctrl->busy);
  133. // Try to send next control message
  134. usb_next_ctrl_msg(urb, adapter);
  135. return;
  136. }
  137. /* ======================================================================
  138. * interrupt pipe
  139. */
  140. /*
  141. * The interrupt endpoint will be called when any
  142. * of the 6 registers changes state (depending on masks).
  143. * Decode the register values and schedule a private event.
  144. * Called at interrupt.
  145. */
  146. static void usb_int_complete(struct urb *urb)
  147. {
  148. u8 *data = urb->transfer_buffer;
  149. u8 irqbyte;
  150. struct st5481_adapter *adapter = urb->context;
  151. int j;
  152. int status;
  153. switch (urb->status) {
  154. case 0:
  155. /* success */
  156. break;
  157. case -ECONNRESET:
  158. case -ENOENT:
  159. case -ESHUTDOWN:
  160. /* this urb is terminated, clean up */
  161. DBG(2, "urb shutting down with status: %d", urb->status);
  162. return;
  163. default:
  164. WARNING("nonzero urb status received: %d", urb->status);
  165. goto exit;
  166. }
  167. DBG_PACKET(2, data, INT_PKT_SIZE);
  168. if (urb->actual_length == 0) {
  169. goto exit;
  170. }
  171. irqbyte = data[MPINT];
  172. if (irqbyte & DEN_INT)
  173. FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
  174. if (irqbyte & DCOLL_INT)
  175. FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
  176. irqbyte = data[FFINT_D];
  177. if (irqbyte & OUT_UNDERRUN)
  178. FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
  179. if (irqbyte & OUT_DOWN)
  180. ;// printk("OUT_DOWN\n");
  181. irqbyte = data[MPINT];
  182. if (irqbyte & RXCI_INT)
  183. FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
  184. for (j = 0; j < 2; j++)
  185. adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
  186. urb->actual_length = 0;
  187. exit:
  188. status = usb_submit_urb (urb, GFP_ATOMIC);
  189. if (status)
  190. WARNING("usb_submit_urb failed with result %d", status);
  191. }
  192. /* ======================================================================
  193. * initialization
  194. */
  195. int st5481_setup_usb(struct st5481_adapter *adapter)
  196. {
  197. struct usb_device *dev = adapter->usb_dev;
  198. struct st5481_ctrl *ctrl = &adapter->ctrl;
  199. struct st5481_intr *intr = &adapter->intr;
  200. struct usb_interface *intf;
  201. struct usb_host_interface *altsetting = NULL;
  202. struct usb_host_endpoint *endpoint;
  203. int status;
  204. struct urb *urb;
  205. u8 *buf;
  206. DBG(2,"");
  207. if ((status = usb_reset_configuration (dev)) < 0) {
  208. WARNING("reset_configuration failed,status=%d",status);
  209. return status;
  210. }
  211. intf = usb_ifnum_to_if(dev, 0);
  212. if (intf)
  213. altsetting = usb_altnum_to_altsetting(intf, 3);
  214. if (!altsetting)
  215. return -ENXIO;
  216. // Check if the config is sane
  217. if ( altsetting->desc.bNumEndpoints != 7 ) {
  218. WARNING("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
  219. return -EINVAL;
  220. }
  221. // The descriptor is wrong for some early samples of the ST5481 chip
  222. altsetting->endpoint[3].desc.wMaxPacketSize = __constant_cpu_to_le16(32);
  223. altsetting->endpoint[4].desc.wMaxPacketSize = __constant_cpu_to_le16(32);
  224. // Use alternative setting 3 on interface 0 to have 2B+D
  225. if ((status = usb_set_interface (dev, 0, 3)) < 0) {
  226. WARNING("usb_set_interface failed,status=%d",status);
  227. return status;
  228. }
  229. // Allocate URB for control endpoint
  230. urb = usb_alloc_urb(0, GFP_KERNEL);
  231. if (!urb) {
  232. return -ENOMEM;
  233. }
  234. ctrl->urb = urb;
  235. // Fill the control URB
  236. usb_fill_control_urb (urb, dev,
  237. usb_sndctrlpipe(dev, 0),
  238. NULL, NULL, 0, usb_ctrl_complete, adapter);
  239. fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
  240. // Allocate URBs and buffers for interrupt endpoint
  241. urb = usb_alloc_urb(0, GFP_KERNEL);
  242. if (!urb) {
  243. return -ENOMEM;
  244. }
  245. intr->urb = urb;
  246. buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
  247. if (!buf) {
  248. return -ENOMEM;
  249. }
  250. endpoint = &altsetting->endpoint[EP_INT-1];
  251. // Fill the interrupt URB
  252. usb_fill_int_urb(urb, dev,
  253. usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
  254. buf, INT_PKT_SIZE,
  255. usb_int_complete, adapter,
  256. endpoint->desc.bInterval);
  257. return 0;
  258. }
  259. /*
  260. * Release buffers and URBs for the interrupt and control
  261. * endpoint.
  262. */
  263. void st5481_release_usb(struct st5481_adapter *adapter)
  264. {
  265. struct st5481_intr *intr = &adapter->intr;
  266. struct st5481_ctrl *ctrl = &adapter->ctrl;
  267. DBG(1,"");
  268. // Stop and free Control and Interrupt URBs
  269. usb_kill_urb(ctrl->urb);
  270. kfree(ctrl->urb->transfer_buffer);
  271. usb_free_urb(ctrl->urb);
  272. ctrl->urb = NULL;
  273. usb_kill_urb(intr->urb);
  274. kfree(intr->urb->transfer_buffer);
  275. usb_free_urb(intr->urb);
  276. intr->urb = NULL;
  277. }
  278. /*
  279. * Initialize the adapter.
  280. */
  281. void st5481_start(struct st5481_adapter *adapter)
  282. {
  283. static const u8 init_cmd_table[]={
  284. SET_DEFAULT,0,
  285. STT,0,
  286. SDA_MIN,0x0d,
  287. SDA_MAX,0x29,
  288. SDELAY_VALUE,0x14,
  289. GPIO_DIR,0x01,
  290. GPIO_OUT,RED_LED,
  291. // FFCTRL_OUT_D,4,
  292. // FFCTRH_OUT_D,12,
  293. FFCTRL_OUT_B1,6,
  294. FFCTRH_OUT_B1,20,
  295. FFCTRL_OUT_B2,6,
  296. FFCTRH_OUT_B2,20,
  297. MPMSK,RXCI_INT+DEN_INT+DCOLL_INT,
  298. 0
  299. };
  300. struct st5481_intr *intr = &adapter->intr;
  301. int i = 0;
  302. u8 request,value;
  303. DBG(8,"");
  304. adapter->leds = RED_LED;
  305. // Start receiving on the interrupt endpoint
  306. SUBMIT_URB(intr->urb, GFP_KERNEL);
  307. while ((request = init_cmd_table[i++])) {
  308. value = init_cmd_table[i++];
  309. st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
  310. }
  311. st5481_ph_command(adapter, ST5481_CMD_PUP);
  312. }
  313. /*
  314. * Reset the adapter to default values.
  315. */
  316. void st5481_stop(struct st5481_adapter *adapter)
  317. {
  318. DBG(8,"");
  319. st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
  320. }
  321. /* ======================================================================
  322. * isochronous USB helpers
  323. */
  324. static void
  325. fill_isoc_urb(struct urb *urb, struct usb_device *dev,
  326. unsigned int pipe, void *buf, int num_packets,
  327. int packet_size, usb_complete_t complete,
  328. void *context)
  329. {
  330. int k;
  331. urb->dev=dev;
  332. urb->pipe=pipe;
  333. urb->interval = 1;
  334. urb->transfer_buffer=buf;
  335. urb->number_of_packets = num_packets;
  336. urb->transfer_buffer_length=num_packets*packet_size;
  337. urb->actual_length = 0;
  338. urb->complete=complete;
  339. urb->context=context;
  340. urb->transfer_flags=URB_ISO_ASAP;
  341. for (k = 0; k < num_packets; k++) {
  342. urb->iso_frame_desc[k].offset = packet_size * k;
  343. urb->iso_frame_desc[k].length = packet_size;
  344. urb->iso_frame_desc[k].actual_length = 0;
  345. }
  346. }
  347. int
  348. st5481_setup_isocpipes(struct urb* urb[2], struct usb_device *dev,
  349. unsigned int pipe, int num_packets,
  350. int packet_size, int buf_size,
  351. usb_complete_t complete, void *context)
  352. {
  353. int j, retval;
  354. unsigned char *buf;
  355. for (j = 0; j < 2; j++) {
  356. retval = -ENOMEM;
  357. urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
  358. if (!urb[j])
  359. goto err;
  360. // Allocate memory for 2000bytes/sec (16Kb/s)
  361. buf = kmalloc(buf_size, GFP_KERNEL);
  362. if (!buf)
  363. goto err;
  364. // Fill the isochronous URB
  365. fill_isoc_urb(urb[j], dev, pipe, buf,
  366. num_packets, packet_size, complete,
  367. context);
  368. }
  369. return 0;
  370. err:
  371. for (j = 0; j < 2; j++) {
  372. if (urb[j]) {
  373. kfree(urb[j]->transfer_buffer);
  374. urb[j]->transfer_buffer = NULL;
  375. usb_free_urb(urb[j]);
  376. urb[j] = NULL;
  377. }
  378. }
  379. return retval;
  380. }
  381. void st5481_release_isocpipes(struct urb* urb[2])
  382. {
  383. int j;
  384. for (j = 0; j < 2; j++) {
  385. usb_kill_urb(urb[j]);
  386. kfree(urb[j]->transfer_buffer);
  387. usb_free_urb(urb[j]);
  388. urb[j] = NULL;
  389. }
  390. }
  391. /*
  392. * Decode frames received on the B/D channel.
  393. * Note that this function will be called continuously
  394. * with 64Kbit/s / 16Kbit/s of data and hence it will be
  395. * called 50 times per second with 20 ISOC descriptors.
  396. * Called at interrupt.
  397. */
  398. static void usb_in_complete(struct urb *urb)
  399. {
  400. struct st5481_in *in = urb->context;
  401. unsigned char *ptr;
  402. struct sk_buff *skb;
  403. int len, count, status;
  404. if (unlikely(urb->status < 0)) {
  405. switch (urb->status) {
  406. case -ENOENT:
  407. case -ESHUTDOWN:
  408. case -ECONNRESET:
  409. DBG(1,"urb killed status %d", urb->status);
  410. return; // Give up
  411. default:
  412. WARNING("urb status %d",urb->status);
  413. break;
  414. }
  415. }
  416. DBG_ISO_PACKET(0x80,urb);
  417. len = st5481_isoc_flatten(urb);
  418. ptr = urb->transfer_buffer;
  419. while (len > 0) {
  420. if (in->mode == L1_MODE_TRANS) {
  421. memcpy(in->rcvbuf, ptr, len);
  422. status = len;
  423. len = 0;
  424. } else {
  425. status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
  426. in->rcvbuf, in->bufsize);
  427. ptr += count;
  428. len -= count;
  429. }
  430. if (status > 0) {
  431. // Good frame received
  432. DBG(4,"count=%d",status);
  433. DBG_PACKET(0x400, in->rcvbuf, status);
  434. if (!(skb = dev_alloc_skb(status))) {
  435. WARNING("receive out of memory\n");
  436. break;
  437. }
  438. memcpy(skb_put(skb, status), in->rcvbuf, status);
  439. in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
  440. } else if (status == -HDLC_CRC_ERROR) {
  441. INFO("CRC error");
  442. } else if (status == -HDLC_FRAMING_ERROR) {
  443. INFO("framing error");
  444. } else if (status == -HDLC_LENGTH_ERROR) {
  445. INFO("length error");
  446. }
  447. }
  448. // Prepare URB for next transfer
  449. urb->dev = in->adapter->usb_dev;
  450. urb->actual_length = 0;
  451. SUBMIT_URB(urb, GFP_ATOMIC);
  452. }
  453. int st5481_setup_in(struct st5481_in *in)
  454. {
  455. struct usb_device *dev = in->adapter->usb_dev;
  456. int retval;
  457. DBG(4,"");
  458. in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
  459. retval = -ENOMEM;
  460. if (!in->rcvbuf)
  461. goto err;
  462. retval = st5481_setup_isocpipes(in->urb, dev,
  463. usb_rcvisocpipe(dev, in->ep),
  464. in->num_packets, in->packet_size,
  465. in->num_packets * in->packet_size,
  466. usb_in_complete, in);
  467. if (retval)
  468. goto err_free;
  469. return 0;
  470. err_free:
  471. kfree(in->rcvbuf);
  472. err:
  473. return retval;
  474. }
  475. void st5481_release_in(struct st5481_in *in)
  476. {
  477. DBG(2,"");
  478. st5481_release_isocpipes(in->urb);
  479. }
  480. /*
  481. * Make the transfer_buffer contiguous by
  482. * copying from the iso descriptors if necessary.
  483. */
  484. static int st5481_isoc_flatten(struct urb *urb)
  485. {
  486. struct usb_iso_packet_descriptor *pipd,*pend;
  487. unsigned char *src,*dst;
  488. unsigned int len;
  489. if (urb->status < 0) {
  490. return urb->status;
  491. }
  492. for (pipd = &urb->iso_frame_desc[0],
  493. pend = &urb->iso_frame_desc[urb->number_of_packets],
  494. dst = urb->transfer_buffer;
  495. pipd < pend;
  496. pipd++) {
  497. if (pipd->status < 0) {
  498. return (pipd->status);
  499. }
  500. len = pipd->actual_length;
  501. pipd->actual_length = 0;
  502. src = urb->transfer_buffer+pipd->offset;
  503. if (src != dst) {
  504. // Need to copy since isoc buffers not full
  505. while (len--) {
  506. *dst++ = *src++;
  507. }
  508. } else {
  509. // No need to copy, just update destination buffer
  510. dst += len;
  511. }
  512. }
  513. // Return size of flattened buffer
  514. return (dst - (unsigned char *)urb->transfer_buffer);
  515. }
  516. static void st5481_start_rcv(void *context)
  517. {
  518. struct st5481_in *in = context;
  519. struct st5481_adapter *adapter = in->adapter;
  520. DBG(4,"");
  521. in->urb[0]->dev = adapter->usb_dev;
  522. SUBMIT_URB(in->urb[0], GFP_KERNEL);
  523. in->urb[1]->dev = adapter->usb_dev;
  524. SUBMIT_URB(in->urb[1], GFP_KERNEL);
  525. }
  526. void st5481_in_mode(struct st5481_in *in, int mode)
  527. {
  528. if (in->mode == mode)
  529. return;
  530. in->mode = mode;
  531. usb_unlink_urb(in->urb[0]);
  532. usb_unlink_urb(in->urb[1]);
  533. if (in->mode != L1_MODE_NULL) {
  534. if (in->mode != L1_MODE_TRANS) {
  535. u32 features = HDLC_BITREVERSE;
  536. if (in->mode == L1_MODE_HDLC_56K)
  537. features |= HDLC_56KBIT;
  538. isdnhdlc_rcv_init(&in->hdlc_state, features);
  539. }
  540. st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
  541. st5481_usb_device_ctrl_msg(in->adapter, in->counter,
  542. in->packet_size,
  543. NULL, NULL);
  544. st5481_start_rcv(in);
  545. } else {
  546. st5481_usb_device_ctrl_msg(in->adapter, in->counter,
  547. 0, NULL, NULL);
  548. }
  549. }