ksdazzle-sir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*****************************************************************************
  2. *
  3. * Filename: ksdazzle.c
  4. * Version: 0.1.2
  5. * Description: Irda KingSun Dazzle USB Dongle
  6. * Status: Experimental
  7. * Author: Alex Villacís Lasso <a_villacis@palosanto.com>
  8. *
  9. * Based on stir4200, mcs7780, kingsun-sir drivers.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. *****************************************************************************/
  25. /*
  26. * Following is my most current (2007-07-26) understanding of how the Kingsun
  27. * 07D0:4100 dongle (sometimes known as the MA-660) is supposed to work. This
  28. * information was deduced by examining the USB traffic captured with USBSnoopy
  29. * from the WinXP driver. Feel free to update here as more of the dongle is
  30. * known.
  31. *
  32. * General: This dongle exposes one interface with two interrupt endpoints, one
  33. * IN and one OUT. In this regard, it is similar to what the Kingsun/Donshine
  34. * dongle (07c0:4200) exposes. Traffic is raw and needs to be wrapped and
  35. * unwrapped manually as in stir4200, kingsun-sir, and ks959-sir.
  36. *
  37. * Transmission: To transmit an IrDA frame, it is necessary to wrap it, then
  38. * split it into multiple segments of up to 7 bytes each, and transmit each in
  39. * sequence. It seems that sending a single big block (like kingsun-sir does)
  40. * won't work with this dongle. Each segment needs to be prefixed with a value
  41. * equal to (unsigned char)0xF8 + <number of bytes in segment>, inside a payload
  42. * of exactly 8 bytes. For example, a segment of 1 byte gets prefixed by 0xF9,
  43. * and one of 7 bytes gets prefixed by 0xFF. The bytes at the end of the
  44. * payload, not considered by the prefix, are ignored (set to 0 by this
  45. * implementation).
  46. *
  47. * Reception: To receive data, the driver must poll the dongle regularly (like
  48. * kingsun-sir.c) with interrupt URBs. If data is available, it will be returned
  49. * in payloads from 0 to 8 bytes long. When concatenated, these payloads form
  50. * a raw IrDA stream that needs to be unwrapped as in stir4200 and kingsun-sir
  51. *
  52. * Speed change: To change the speed of the dongle, the driver prepares a
  53. * control URB with the following as a setup packet:
  54. * bRequestType USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
  55. * bRequest 0x09
  56. * wValue 0x0200
  57. * wIndex 0x0001
  58. * wLength 0x0008 (length of the payload)
  59. * The payload is a 8-byte record, apparently identical to the one used in
  60. * drivers/usb/serial/cypress_m8.c to change speed:
  61. * __u32 baudSpeed;
  62. * unsigned int dataBits : 2; // 0 - 5 bits 3 - 8 bits
  63. * unsigned int : 1;
  64. * unsigned int stopBits : 1;
  65. * unsigned int parityEnable : 1;
  66. * unsigned int parityType : 1;
  67. * unsigned int : 1;
  68. * unsigned int reset : 1;
  69. * unsigned char reserved[3]; // set to 0
  70. *
  71. * For now only SIR speeds have been observed with this dongle. Therefore,
  72. * nothing is known on what changes (if any) must be done to frame wrapping /
  73. * unwrapping for higher than SIR speeds. This driver assumes no change is
  74. * necessary and announces support for all the way to 115200 bps.
  75. */
  76. #include <linux/module.h>
  77. #include <linux/moduleparam.h>
  78. #include <linux/kernel.h>
  79. #include <linux/types.h>
  80. #include <linux/errno.h>
  81. #include <linux/slab.h>
  82. #include <linux/usb.h>
  83. #include <linux/device.h>
  84. #include <linux/crc32.h>
  85. #include <asm/unaligned.h>
  86. #include <asm/byteorder.h>
  87. #include <asm/uaccess.h>
  88. #include <net/irda/irda.h>
  89. #include <net/irda/wrapper.h>
  90. #include <net/irda/crc.h>
  91. #define KSDAZZLE_VENDOR_ID 0x07d0
  92. #define KSDAZZLE_PRODUCT_ID 0x4100
  93. /* These are the currently known USB ids */
  94. static struct usb_device_id dongles[] = {
  95. /* KingSun Co,Ltd IrDA/USB Bridge */
  96. {USB_DEVICE(KSDAZZLE_VENDOR_ID, KSDAZZLE_PRODUCT_ID)},
  97. {}
  98. };
  99. MODULE_DEVICE_TABLE(usb, dongles);
  100. #define KINGSUN_MTT 0x07
  101. #define KINGSUN_REQ_RECV 0x01
  102. #define KINGSUN_REQ_SEND 0x09
  103. #define KINGSUN_SND_FIFO_SIZE 2048 /* Max packet we can send */
  104. #define KINGSUN_RCV_MAX 2048 /* Max transfer we can receive */
  105. struct ksdazzle_speedparams {
  106. __le32 baudrate; /* baud rate, little endian */
  107. __u8 flags;
  108. __u8 reserved[3];
  109. } __packed;
  110. #define KS_DATA_5_BITS 0x00
  111. #define KS_DATA_6_BITS 0x01
  112. #define KS_DATA_7_BITS 0x02
  113. #define KS_DATA_8_BITS 0x03
  114. #define KS_STOP_BITS_1 0x00
  115. #define KS_STOP_BITS_2 0x08
  116. #define KS_PAR_DISABLE 0x00
  117. #define KS_PAR_EVEN 0x10
  118. #define KS_PAR_ODD 0x30
  119. #define KS_RESET 0x80
  120. #define KINGSUN_EP_IN 0
  121. #define KINGSUN_EP_OUT 1
  122. struct ksdazzle_cb {
  123. struct usb_device *usbdev; /* init: probe_irda */
  124. struct net_device *netdev; /* network layer */
  125. struct irlap_cb *irlap; /* The link layer we are binded to */
  126. struct qos_info qos;
  127. struct urb *tx_urb;
  128. __u8 *tx_buf_clear;
  129. unsigned int tx_buf_clear_used;
  130. unsigned int tx_buf_clear_sent;
  131. __u8 tx_payload[8];
  132. struct urb *rx_urb;
  133. __u8 *rx_buf;
  134. iobuff_t rx_unwrap_buff;
  135. struct usb_ctrlrequest *speed_setuprequest;
  136. struct urb *speed_urb;
  137. struct ksdazzle_speedparams speedparams;
  138. unsigned int new_speed;
  139. __u8 ep_in;
  140. __u8 ep_out;
  141. spinlock_t lock;
  142. int receiving;
  143. };
  144. /* Callback transmission routine */
  145. static void ksdazzle_speed_irq(struct urb *urb)
  146. {
  147. /* unlink, shutdown, unplug, other nasties */
  148. if (urb->status != 0)
  149. dev_err(&urb->dev->dev,
  150. "ksdazzle_speed_irq: urb asynchronously failed - %d\n",
  151. urb->status);
  152. }
  153. /* Send a control request to change speed of the dongle */
  154. static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
  155. {
  156. static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
  157. 57600, 115200, 576000, 1152000, 4000000, 0
  158. };
  159. int err;
  160. unsigned int i;
  161. if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
  162. return -ENOMEM;
  163. /* Check that requested speed is among the supported ones */
  164. for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
  165. if (supported_speeds[i] == 0)
  166. return -EOPNOTSUPP;
  167. memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
  168. kingsun->speedparams.baudrate = cpu_to_le32(speed);
  169. kingsun->speedparams.flags = KS_DATA_8_BITS;
  170. /* speed_setuprequest pre-filled in ksdazzle_probe */
  171. usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
  172. usb_sndctrlpipe(kingsun->usbdev, 0),
  173. (unsigned char *)kingsun->speed_setuprequest,
  174. &(kingsun->speedparams),
  175. sizeof(struct ksdazzle_speedparams),
  176. ksdazzle_speed_irq, kingsun);
  177. kingsun->speed_urb->status = 0;
  178. err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
  179. return err;
  180. }
  181. /* Submit one fragment of an IrDA frame to the dongle */
  182. static void ksdazzle_send_irq(struct urb *urb);
  183. static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
  184. {
  185. unsigned int wraplen;
  186. int ret;
  187. /* We can send at most 7 bytes of payload at a time */
  188. wraplen = 7;
  189. if (wraplen > kingsun->tx_buf_clear_used)
  190. wraplen = kingsun->tx_buf_clear_used;
  191. /* Prepare payload prefix with used length */
  192. memset(kingsun->tx_payload, 0, 8);
  193. kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
  194. memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
  195. usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
  196. usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
  197. kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
  198. kingsun->tx_urb->status = 0;
  199. ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
  200. /* Remember how much data was sent, in order to update at callback */
  201. kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
  202. return ret;
  203. }
  204. /* Callback transmission routine */
  205. static void ksdazzle_send_irq(struct urb *urb)
  206. {
  207. struct ksdazzle_cb *kingsun = urb->context;
  208. struct net_device *netdev = kingsun->netdev;
  209. int ret = 0;
  210. /* in process of stopping, just drop data */
  211. if (!netif_running(kingsun->netdev)) {
  212. dev_err(&kingsun->usbdev->dev,
  213. "ksdazzle_send_irq: Network not running!\n");
  214. return;
  215. }
  216. /* unlink, shutdown, unplug, other nasties */
  217. if (urb->status != 0) {
  218. dev_err(&kingsun->usbdev->dev,
  219. "ksdazzle_send_irq: urb asynchronously failed - %d\n",
  220. urb->status);
  221. return;
  222. }
  223. if (kingsun->tx_buf_clear_used > 0) {
  224. /* Update data remaining to be sent */
  225. if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
  226. memmove(kingsun->tx_buf_clear,
  227. kingsun->tx_buf_clear +
  228. kingsun->tx_buf_clear_sent,
  229. kingsun->tx_buf_clear_used -
  230. kingsun->tx_buf_clear_sent);
  231. }
  232. kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
  233. kingsun->tx_buf_clear_sent = 0;
  234. if (kingsun->tx_buf_clear_used > 0) {
  235. /* There is more data to be sent */
  236. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  237. dev_err(&kingsun->usbdev->dev,
  238. "ksdazzle_send_irq: failed tx_urb submit: %d\n",
  239. ret);
  240. switch (ret) {
  241. case -ENODEV:
  242. case -EPIPE:
  243. break;
  244. default:
  245. netdev->stats.tx_errors++;
  246. netif_start_queue(netdev);
  247. }
  248. }
  249. } else {
  250. /* All data sent, send next speed && wake network queue */
  251. if (kingsun->new_speed != -1 &&
  252. cpu_to_le32(kingsun->new_speed) !=
  253. kingsun->speedparams.baudrate)
  254. ksdazzle_change_speed(kingsun,
  255. kingsun->new_speed);
  256. netif_wake_queue(netdev);
  257. }
  258. }
  259. }
  260. /*
  261. * Called from net/core when new frame is available.
  262. */
  263. static netdev_tx_t ksdazzle_hard_xmit(struct sk_buff *skb,
  264. struct net_device *netdev)
  265. {
  266. struct ksdazzle_cb *kingsun;
  267. unsigned int wraplen;
  268. int ret = 0;
  269. netif_stop_queue(netdev);
  270. /* the IRDA wrapping routines don't deal with non linear skb */
  271. SKB_LINEAR_ASSERT(skb);
  272. kingsun = netdev_priv(netdev);
  273. spin_lock(&kingsun->lock);
  274. kingsun->new_speed = irda_get_next_speed(skb);
  275. /* Append data to the end of whatever data remains to be transmitted */
  276. wraplen =
  277. async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
  278. kingsun->tx_buf_clear_used = wraplen;
  279. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  280. dev_err(&kingsun->usbdev->dev,
  281. "ksdazzle_hard_xmit: failed tx_urb submit: %d\n", ret);
  282. switch (ret) {
  283. case -ENODEV:
  284. case -EPIPE:
  285. break;
  286. default:
  287. netdev->stats.tx_errors++;
  288. netif_start_queue(netdev);
  289. }
  290. } else {
  291. netdev->stats.tx_packets++;
  292. netdev->stats.tx_bytes += skb->len;
  293. }
  294. dev_kfree_skb(skb);
  295. spin_unlock(&kingsun->lock);
  296. return NETDEV_TX_OK;
  297. }
  298. /* Receive callback function */
  299. static void ksdazzle_rcv_irq(struct urb *urb)
  300. {
  301. struct ksdazzle_cb *kingsun = urb->context;
  302. struct net_device *netdev = kingsun->netdev;
  303. /* in process of stopping, just drop data */
  304. if (!netif_running(netdev)) {
  305. kingsun->receiving = 0;
  306. return;
  307. }
  308. /* unlink, shutdown, unplug, other nasties */
  309. if (urb->status != 0) {
  310. dev_err(&kingsun->usbdev->dev,
  311. "ksdazzle_rcv_irq: urb asynchronously failed - %d\n",
  312. urb->status);
  313. kingsun->receiving = 0;
  314. return;
  315. }
  316. if (urb->actual_length > 0) {
  317. __u8 *bytes = urb->transfer_buffer;
  318. unsigned int i;
  319. for (i = 0; i < urb->actual_length; i++) {
  320. async_unwrap_char(netdev, &netdev->stats,
  321. &kingsun->rx_unwrap_buff, bytes[i]);
  322. }
  323. kingsun->receiving =
  324. (kingsun->rx_unwrap_buff.state != OUTSIDE_FRAME) ? 1 : 0;
  325. }
  326. /* This urb has already been filled in ksdazzle_net_open. It is assumed that
  327. urb keeps the pointer to the payload buffer.
  328. */
  329. urb->status = 0;
  330. usb_submit_urb(urb, GFP_ATOMIC);
  331. }
  332. /*
  333. * Function ksdazzle_net_open (dev)
  334. *
  335. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  336. */
  337. static int ksdazzle_net_open(struct net_device *netdev)
  338. {
  339. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  340. int err = -ENOMEM;
  341. char hwname[16];
  342. /* At this point, urbs are NULL, and skb is NULL (see ksdazzle_probe) */
  343. kingsun->receiving = 0;
  344. /* Initialize for SIR to copy data directly into skb. */
  345. kingsun->rx_unwrap_buff.in_frame = FALSE;
  346. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  347. kingsun->rx_unwrap_buff.truesize = IRDA_SKB_MAX_MTU;
  348. kingsun->rx_unwrap_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  349. if (!kingsun->rx_unwrap_buff.skb)
  350. goto free_mem;
  351. skb_reserve(kingsun->rx_unwrap_buff.skb, 1);
  352. kingsun->rx_unwrap_buff.head = kingsun->rx_unwrap_buff.skb->data;
  353. kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  354. if (!kingsun->rx_urb)
  355. goto free_mem;
  356. kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  357. if (!kingsun->tx_urb)
  358. goto free_mem;
  359. kingsun->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
  360. if (!kingsun->speed_urb)
  361. goto free_mem;
  362. /* Initialize speed for dongle */
  363. kingsun->new_speed = 9600;
  364. err = ksdazzle_change_speed(kingsun, 9600);
  365. if (err < 0)
  366. goto free_mem;
  367. /*
  368. * Now that everything should be initialized properly,
  369. * Open new IrLAP layer instance to take care of us...
  370. */
  371. sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
  372. kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
  373. if (!kingsun->irlap) {
  374. err = -ENOMEM;
  375. dev_err(&kingsun->usbdev->dev, "irlap_open failed\n");
  376. goto free_mem;
  377. }
  378. /* Start reception. */
  379. usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
  380. usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
  381. kingsun->rx_buf, KINGSUN_RCV_MAX, ksdazzle_rcv_irq,
  382. kingsun, 1);
  383. kingsun->rx_urb->status = 0;
  384. err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  385. if (err) {
  386. dev_err(&kingsun->usbdev->dev, "first urb-submit failed: %d\n", err);
  387. goto close_irlap;
  388. }
  389. netif_start_queue(netdev);
  390. /* Situation at this point:
  391. - all work buffers allocated
  392. - urbs allocated and ready to fill
  393. - max rx packet known (in max_rx)
  394. - unwrap state machine initialized, in state outside of any frame
  395. - receive request in progress
  396. - IrLAP layer started, about to hand over packets to send
  397. */
  398. return 0;
  399. close_irlap:
  400. irlap_close(kingsun->irlap);
  401. free_mem:
  402. usb_free_urb(kingsun->speed_urb);
  403. kingsun->speed_urb = NULL;
  404. usb_free_urb(kingsun->tx_urb);
  405. kingsun->tx_urb = NULL;
  406. usb_free_urb(kingsun->rx_urb);
  407. kingsun->rx_urb = NULL;
  408. if (kingsun->rx_unwrap_buff.skb) {
  409. kfree_skb(kingsun->rx_unwrap_buff.skb);
  410. kingsun->rx_unwrap_buff.skb = NULL;
  411. kingsun->rx_unwrap_buff.head = NULL;
  412. }
  413. return err;
  414. }
  415. /*
  416. * Function ksdazzle_net_close (dev)
  417. *
  418. * Network device is taken down. Usually this is done by
  419. * "ifconfig irda0 down"
  420. */
  421. static int ksdazzle_net_close(struct net_device *netdev)
  422. {
  423. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  424. /* Stop transmit processing */
  425. netif_stop_queue(netdev);
  426. /* Mop up receive && transmit urb's */
  427. usb_kill_urb(kingsun->tx_urb);
  428. usb_free_urb(kingsun->tx_urb);
  429. kingsun->tx_urb = NULL;
  430. usb_kill_urb(kingsun->speed_urb);
  431. usb_free_urb(kingsun->speed_urb);
  432. kingsun->speed_urb = NULL;
  433. usb_kill_urb(kingsun->rx_urb);
  434. usb_free_urb(kingsun->rx_urb);
  435. kingsun->rx_urb = NULL;
  436. kfree_skb(kingsun->rx_unwrap_buff.skb);
  437. kingsun->rx_unwrap_buff.skb = NULL;
  438. kingsun->rx_unwrap_buff.head = NULL;
  439. kingsun->rx_unwrap_buff.in_frame = FALSE;
  440. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  441. kingsun->receiving = 0;
  442. /* Stop and remove instance of IrLAP */
  443. irlap_close(kingsun->irlap);
  444. kingsun->irlap = NULL;
  445. return 0;
  446. }
  447. /*
  448. * IOCTLs : Extra out-of-band network commands...
  449. */
  450. static int ksdazzle_net_ioctl(struct net_device *netdev, struct ifreq *rq,
  451. int cmd)
  452. {
  453. struct if_irda_req *irq = (struct if_irda_req *)rq;
  454. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  455. int ret = 0;
  456. switch (cmd) {
  457. case SIOCSBANDWIDTH: /* Set bandwidth */
  458. if (!capable(CAP_NET_ADMIN))
  459. return -EPERM;
  460. /* Check if the device is still there */
  461. if (netif_device_present(kingsun->netdev))
  462. return ksdazzle_change_speed(kingsun,
  463. irq->ifr_baudrate);
  464. break;
  465. case SIOCSMEDIABUSY: /* Set media busy */
  466. if (!capable(CAP_NET_ADMIN))
  467. return -EPERM;
  468. /* Check if the IrDA stack is still there */
  469. if (netif_running(kingsun->netdev))
  470. irda_device_set_media_busy(kingsun->netdev, TRUE);
  471. break;
  472. case SIOCGRECEIVING:
  473. /* Only approximately true */
  474. irq->ifr_receiving = kingsun->receiving;
  475. break;
  476. default:
  477. ret = -EOPNOTSUPP;
  478. }
  479. return ret;
  480. }
  481. static const struct net_device_ops ksdazzle_ops = {
  482. .ndo_start_xmit = ksdazzle_hard_xmit,
  483. .ndo_open = ksdazzle_net_open,
  484. .ndo_stop = ksdazzle_net_close,
  485. .ndo_do_ioctl = ksdazzle_net_ioctl,
  486. };
  487. /*
  488. * This routine is called by the USB subsystem for each new device
  489. * in the system. We need to check if the device is ours, and in
  490. * this case start handling it.
  491. */
  492. static int ksdazzle_probe(struct usb_interface *intf,
  493. const struct usb_device_id *id)
  494. {
  495. struct usb_host_interface *interface;
  496. struct usb_endpoint_descriptor *endpoint;
  497. struct usb_device *dev = interface_to_usbdev(intf);
  498. struct ksdazzle_cb *kingsun = NULL;
  499. struct net_device *net = NULL;
  500. int ret = -ENOMEM;
  501. int pipe, maxp_in, maxp_out;
  502. __u8 ep_in;
  503. __u8 ep_out;
  504. /* Check that there really are two interrupt endpoints. Check based on the
  505. one in drivers/usb/input/usbmouse.c
  506. */
  507. interface = intf->cur_altsetting;
  508. if (interface->desc.bNumEndpoints != 2) {
  509. dev_err(&intf->dev, "ksdazzle: expected 2 endpoints, found %d\n",
  510. interface->desc.bNumEndpoints);
  511. return -ENODEV;
  512. }
  513. endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
  514. if (!usb_endpoint_is_int_in(endpoint)) {
  515. dev_err(&intf->dev,
  516. "ksdazzle: endpoint 0 is not interrupt IN\n");
  517. return -ENODEV;
  518. }
  519. ep_in = endpoint->bEndpointAddress;
  520. pipe = usb_rcvintpipe(dev, ep_in);
  521. maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  522. if (maxp_in > 255 || maxp_in <= 1) {
  523. dev_err(&intf->dev,
  524. "ksdazzle: endpoint 0 has max packet size %d not in range [2..255]\n",
  525. maxp_in);
  526. return -ENODEV;
  527. }
  528. endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
  529. if (!usb_endpoint_is_int_out(endpoint)) {
  530. dev_err(&intf->dev,
  531. "ksdazzle: endpoint 1 is not interrupt OUT\n");
  532. return -ENODEV;
  533. }
  534. ep_out = endpoint->bEndpointAddress;
  535. pipe = usb_sndintpipe(dev, ep_out);
  536. maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  537. /* Allocate network device container. */
  538. net = alloc_irdadev(sizeof(*kingsun));
  539. if (!net)
  540. goto err_out1;
  541. SET_NETDEV_DEV(net, &intf->dev);
  542. kingsun = netdev_priv(net);
  543. kingsun->netdev = net;
  544. kingsun->usbdev = dev;
  545. kingsun->ep_in = ep_in;
  546. kingsun->ep_out = ep_out;
  547. kingsun->irlap = NULL;
  548. kingsun->tx_urb = NULL;
  549. kingsun->tx_buf_clear = NULL;
  550. kingsun->tx_buf_clear_used = 0;
  551. kingsun->tx_buf_clear_sent = 0;
  552. kingsun->rx_urb = NULL;
  553. kingsun->rx_buf = NULL;
  554. kingsun->rx_unwrap_buff.in_frame = FALSE;
  555. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  556. kingsun->rx_unwrap_buff.skb = NULL;
  557. kingsun->receiving = 0;
  558. spin_lock_init(&kingsun->lock);
  559. kingsun->speed_setuprequest = NULL;
  560. kingsun->speed_urb = NULL;
  561. kingsun->speedparams.baudrate = 0;
  562. /* Allocate input buffer */
  563. kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
  564. if (!kingsun->rx_buf)
  565. goto free_mem;
  566. /* Allocate output buffer */
  567. kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
  568. if (!kingsun->tx_buf_clear)
  569. goto free_mem;
  570. /* Allocate and initialize speed setup packet */
  571. kingsun->speed_setuprequest =
  572. kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  573. if (!kingsun->speed_setuprequest)
  574. goto free_mem;
  575. kingsun->speed_setuprequest->bRequestType =
  576. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  577. kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
  578. kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
  579. kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
  580. kingsun->speed_setuprequest->wLength =
  581. cpu_to_le16(sizeof(struct ksdazzle_speedparams));
  582. printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
  583. "Vendor: %x, Product: %x\n",
  584. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  585. le16_to_cpu(dev->descriptor.idProduct));
  586. /* Initialize QoS for this device */
  587. irda_init_max_qos_capabilies(&kingsun->qos);
  588. /* Baud rates known to be supported. Please uncomment if devices (other
  589. than a SonyEriccson K300 phone) can be shown to support higher speeds
  590. with this dongle.
  591. */
  592. kingsun->qos.baud_rate.bits =
  593. IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  594. kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
  595. irda_qos_bits_to_value(&kingsun->qos);
  596. /* Override the network functions we need to use */
  597. net->netdev_ops = &ksdazzle_ops;
  598. ret = register_netdev(net);
  599. if (ret != 0)
  600. goto free_mem;
  601. dev_info(&net->dev, "IrDA: Registered KingSun/Dazzle device %s\n",
  602. net->name);
  603. usb_set_intfdata(intf, kingsun);
  604. /* Situation at this point:
  605. - all work buffers allocated
  606. - setup requests pre-filled
  607. - urbs not allocated, set to NULL
  608. - max rx packet known (is KINGSUN_FIFO_SIZE)
  609. - unwrap state machine (partially) initialized, but skb == NULL
  610. */
  611. return 0;
  612. free_mem:
  613. kfree(kingsun->speed_setuprequest);
  614. kfree(kingsun->tx_buf_clear);
  615. kfree(kingsun->rx_buf);
  616. free_netdev(net);
  617. err_out1:
  618. return ret;
  619. }
  620. /*
  621. * The current device is removed, the USB layer tell us to shut it down...
  622. */
  623. static void ksdazzle_disconnect(struct usb_interface *intf)
  624. {
  625. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  626. if (!kingsun)
  627. return;
  628. unregister_netdev(kingsun->netdev);
  629. /* Mop up receive && transmit urb's */
  630. usb_kill_urb(kingsun->speed_urb);
  631. usb_free_urb(kingsun->speed_urb);
  632. kingsun->speed_urb = NULL;
  633. usb_kill_urb(kingsun->tx_urb);
  634. usb_free_urb(kingsun->tx_urb);
  635. kingsun->tx_urb = NULL;
  636. usb_kill_urb(kingsun->rx_urb);
  637. usb_free_urb(kingsun->rx_urb);
  638. kingsun->rx_urb = NULL;
  639. kfree(kingsun->speed_setuprequest);
  640. kfree(kingsun->tx_buf_clear);
  641. kfree(kingsun->rx_buf);
  642. free_netdev(kingsun->netdev);
  643. usb_set_intfdata(intf, NULL);
  644. }
  645. #ifdef CONFIG_PM
  646. /* USB suspend, so power off the transmitter/receiver */
  647. static int ksdazzle_suspend(struct usb_interface *intf, pm_message_t message)
  648. {
  649. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  650. netif_device_detach(kingsun->netdev);
  651. if (kingsun->speed_urb != NULL)
  652. usb_kill_urb(kingsun->speed_urb);
  653. if (kingsun->tx_urb != NULL)
  654. usb_kill_urb(kingsun->tx_urb);
  655. if (kingsun->rx_urb != NULL)
  656. usb_kill_urb(kingsun->rx_urb);
  657. return 0;
  658. }
  659. /* Coming out of suspend, so reset hardware */
  660. static int ksdazzle_resume(struct usb_interface *intf)
  661. {
  662. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  663. if (kingsun->rx_urb != NULL) {
  664. /* Setup request already filled in ksdazzle_probe */
  665. usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  666. }
  667. netif_device_attach(kingsun->netdev);
  668. return 0;
  669. }
  670. #endif
  671. /*
  672. * USB device callbacks
  673. */
  674. static struct usb_driver irda_driver = {
  675. .name = "ksdazzle-sir",
  676. .probe = ksdazzle_probe,
  677. .disconnect = ksdazzle_disconnect,
  678. .id_table = dongles,
  679. #ifdef CONFIG_PM
  680. .suspend = ksdazzle_suspend,
  681. .resume = ksdazzle_resume,
  682. #endif
  683. };
  684. module_usb_driver(irda_driver);
  685. MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
  686. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun Dazzle");
  687. MODULE_LICENSE("GPL");