ipw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * IPWireless 3G UMTS TDD Modem driver (USB connected)
  3. *
  4. * Copyright (C) 2004 Roelf Diedericks <roelfd@inet.co.za>
  5. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * All information about the device was acquired using SnoopyPro
  13. * on MSFT's O/S, and examing the MSFT drivers' debug output
  14. * (insanely left _on_ in the enduser version)
  15. *
  16. * It was written out of frustration with the IPWireless USB modem
  17. * supplied by Axity3G/Sentech South Africa not supporting
  18. * Linux whatsoever.
  19. *
  20. * Nobody provided any proprietary information that was not already
  21. * available for this device.
  22. *
  23. * The modem adheres to the "3GPP TS 27.007 AT command set for 3G
  24. * User Equipment (UE)" standard, available from
  25. * http://www.3gpp.org/ftp/Specs/html-info/27007.htm
  26. *
  27. * The code was only tested the IPWireless handheld modem distributed
  28. * in South Africa by Sentech.
  29. *
  30. * It may work for Woosh Inc in .nz too, as it appears they use the
  31. * same kit.
  32. *
  33. * There is still some work to be done in terms of handling
  34. * DCD, DTR, RTS, CTS which are currently faked.
  35. * It's good enough for PPP at this point. It's based off all kinds of
  36. * code found in usb/serial and usb/class
  37. *
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/errno.h>
  41. #include <linux/init.h>
  42. #include <linux/slab.h>
  43. #include <linux/tty.h>
  44. #include <linux/tty_flip.h>
  45. #include <linux/module.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb.h>
  49. #include <asm/uaccess.h>
  50. #include "usb-serial.h"
  51. /*
  52. * Version Information
  53. */
  54. #define DRIVER_VERSION "v0.3"
  55. #define DRIVER_AUTHOR "Roelf Diedericks"
  56. #define DRIVER_DESC "IPWireless tty driver"
  57. #define IPW_TTY_MAJOR 240 /* real device node major id, experimental range */
  58. #define IPW_TTY_MINORS 256 /* we support 256 devices, dunno why, it'd be insane :) */
  59. #define USB_IPW_MAGIC 0x6d02 /* magic number for ipw struct */
  60. /* Message sizes */
  61. #define EVENT_BUFFER_SIZE 0xFF
  62. #define CHAR2INT16(c1,c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
  63. #define NUM_BULK_URBS 24
  64. #define NUM_CONTROL_URBS 16
  65. /* vendor/product pairs that are known work with this driver*/
  66. #define IPW_VID 0x0bc3
  67. #define IPW_PID 0x0001
  68. /* Vendor commands: */
  69. /* baud rates */
  70. enum {
  71. ipw_sio_b256000 = 0x000e,
  72. ipw_sio_b128000 = 0x001d,
  73. ipw_sio_b115200 = 0x0020,
  74. ipw_sio_b57600 = 0x0040,
  75. ipw_sio_b56000 = 0x0042,
  76. ipw_sio_b38400 = 0x0060,
  77. ipw_sio_b19200 = 0x00c0,
  78. ipw_sio_b14400 = 0x0100,
  79. ipw_sio_b9600 = 0x0180,
  80. ipw_sio_b4800 = 0x0300,
  81. ipw_sio_b2400 = 0x0600,
  82. ipw_sio_b1200 = 0x0c00,
  83. ipw_sio_b600 = 0x1800
  84. };
  85. /* data bits */
  86. #define ipw_dtb_7 0x700
  87. #define ipw_dtb_8 0x810 // ok so the define is misleading, I know, but forces 8,n,1
  88. // I mean, is there a point to any other setting these days? :)
  89. /* usb control request types : */
  90. #define IPW_SIO_RXCTL 0x00 // control bulk rx channel transmissions, value=1/0 (on/off)
  91. #define IPW_SIO_SET_BAUD 0x01 // set baud, value=requested ipw_sio_bxxxx
  92. #define IPW_SIO_SET_LINE 0x03 // set databits, parity. value=ipw_dtb_x
  93. #define IPW_SIO_SET_PIN 0x03 // set/clear dtr/rts value=ipw_pin_xxx
  94. #define IPW_SIO_POLL 0x08 // get serial port status byte, call with value=0
  95. #define IPW_SIO_INIT 0x11 // initializes ? value=0 (appears as first thing todo on open)
  96. #define IPW_SIO_PURGE 0x12 // purge all transmissions?, call with value=numchar_to_purge
  97. #define IPW_SIO_HANDFLOW 0x13 // set xon/xoff limits value=0, and a buffer of 0x10 bytes
  98. #define IPW_SIO_SETCHARS 0x13 // set the flowcontrol special chars, value=0, buf=6 bytes,
  99. // last 2 bytes contain flowcontrol chars e.g. 00 00 00 00 11 13
  100. /* values used for request IPW_SIO_SET_PIN */
  101. #define IPW_PIN_SETDTR 0x101
  102. #define IPW_PIN_SETRTS 0x202
  103. #define IPW_PIN_CLRDTR 0x100
  104. #define IPW_PIN_CLRRTS 0x200 // unconfirmed
  105. /* values used for request IPW_SIO_RXCTL */
  106. #define IPW_RXBULK_ON 1
  107. #define IPW_RXBULK_OFF 0
  108. /* various 16 byte hardcoded transferbuffers used by flow control */
  109. #define IPW_BYTES_FLOWINIT { 0x01, 0, 0, 0, 0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  110. /* Interpretation of modem status lines */
  111. /* These need sorting out by individually connecting pins and checking
  112. * results. FIXME!
  113. * When data is being sent we see 0x30 in the lower byte; this must
  114. * contain DSR and CTS ...
  115. */
  116. #define IPW_DSR ((1<<4) | (1<<5))
  117. #define IPW_CTS ((1<<5) | (1<<4))
  118. #define IPW_WANTS_TO_SEND 0x30
  119. //#define IPW_DTR /* Data Terminal Ready */
  120. //#define IPW_CTS /* Clear To Send */
  121. //#define IPW_CD /* Carrier Detect */
  122. //#define IPW_DSR /* Data Set Ready */
  123. //#define IPW_RxD /* Receive pin */
  124. //#define IPW_LE
  125. //#define IPW_RTS
  126. //#define IPW_ST
  127. //#define IPW_SR
  128. //#define IPW_RI /* Ring Indicator */
  129. static struct usb_device_id usb_ipw_ids[] = {
  130. { USB_DEVICE(IPW_VID, IPW_PID) },
  131. { },
  132. };
  133. MODULE_DEVICE_TABLE(usb, usb_ipw_ids);
  134. static struct usb_driver usb_ipw_driver = {
  135. .owner = THIS_MODULE,
  136. .name = "ipwtty",
  137. .probe = usb_serial_probe,
  138. .disconnect = usb_serial_disconnect,
  139. .id_table = usb_ipw_ids,
  140. };
  141. static int debug;
  142. static void ipw_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
  143. {
  144. struct usb_serial_port *port = urb->context;
  145. unsigned char *data = urb->transfer_buffer;
  146. struct tty_struct *tty;
  147. int i;
  148. int result;
  149. dbg("%s - port %d", __FUNCTION__, port->number);
  150. if (urb->status) {
  151. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  152. return;
  153. }
  154. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  155. tty = port->tty;
  156. if (tty && urb->actual_length) {
  157. for (i = 0; i < urb->actual_length ; ++i) {
  158. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  159. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  160. tty_flip_buffer_push(tty);
  161. }
  162. /* this doesn't actually push the data through unless tty->low_latency is set */
  163. tty_insert_flip_char(tty, data[i], 0);
  164. }
  165. tty_flip_buffer_push(tty);
  166. }
  167. /* Continue trying to always read */
  168. usb_fill_bulk_urb (port->read_urb, port->serial->dev,
  169. usb_rcvbulkpipe(port->serial->dev,
  170. port->bulk_in_endpointAddress),
  171. port->read_urb->transfer_buffer,
  172. port->read_urb->transfer_buffer_length,
  173. ipw_read_bulk_callback, port);
  174. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  175. if (result)
  176. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  177. return;
  178. }
  179. static int ipw_open(struct usb_serial_port *port, struct file *filp)
  180. {
  181. struct usb_device *dev = port->serial->dev;
  182. u8 buf_flow_static[16] = IPW_BYTES_FLOWINIT;
  183. u8 *buf_flow_init;
  184. int result;
  185. dbg("%s", __FUNCTION__);
  186. buf_flow_init = kmalloc(16, GFP_KERNEL);
  187. if (!buf_flow_init)
  188. return -ENOMEM;
  189. memcpy(buf_flow_init, buf_flow_static, 16);
  190. if (port->tty)
  191. port->tty->low_latency = 1;
  192. /* --1: Tell the modem to initialize (we think) From sniffs this is always the
  193. * first thing that gets sent to the modem during opening of the device */
  194. dbg("%s: Sending SIO_INIT (we guess)",__FUNCTION__);
  195. result = usb_control_msg(dev, usb_sndctrlpipe(dev,0),
  196. IPW_SIO_INIT,
  197. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  198. 0,
  199. 0, /* index */
  200. NULL,
  201. 0,
  202. 100000);
  203. if (result < 0)
  204. dev_err(&port->dev, "Init of modem failed (error = %d)", result);
  205. /* reset the bulk pipes */
  206. usb_clear_halt(dev, usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress));
  207. usb_clear_halt(dev, usb_sndbulkpipe(dev, port->bulk_out_endpointAddress));
  208. /*--2: Start reading from the device */
  209. dbg("%s: setting up bulk read callback",__FUNCTION__);
  210. usb_fill_bulk_urb(port->read_urb, dev,
  211. usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress),
  212. port->bulk_in_buffer,
  213. port->bulk_in_size,
  214. ipw_read_bulk_callback, port);
  215. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  216. if (result < 0)
  217. dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result);
  218. /*--3: Tell the modem to open the floodgates on the rx bulk channel */
  219. dbg("%s:asking modem for RxRead (RXBULK_ON)",__FUNCTION__);
  220. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  221. IPW_SIO_RXCTL,
  222. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  223. IPW_RXBULK_ON,
  224. 0, /* index */
  225. NULL,
  226. 0,
  227. 100000);
  228. if (result < 0)
  229. dev_err(&port->dev, "Enabling bulk RxRead failed (error = %d)", result);
  230. /*--4: setup the initial flowcontrol */
  231. dbg("%s:setting init flowcontrol (%s)",__FUNCTION__,buf_flow_init);
  232. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  233. IPW_SIO_HANDFLOW,
  234. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  235. 0,
  236. 0,
  237. buf_flow_init,
  238. 0x10,
  239. 200000);
  240. if (result < 0)
  241. dev_err(&port->dev, "initial flowcontrol failed (error = %d)", result);
  242. /*--5: raise the dtr */
  243. dbg("%s:raising dtr",__FUNCTION__);
  244. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  245. IPW_SIO_SET_PIN,
  246. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  247. IPW_PIN_SETDTR,
  248. 0,
  249. NULL,
  250. 0,
  251. 200000);
  252. if (result < 0)
  253. dev_err(&port->dev, "setting dtr failed (error = %d)", result);
  254. /*--6: raise the rts */
  255. dbg("%s:raising rts",__FUNCTION__);
  256. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  257. IPW_SIO_SET_PIN,
  258. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  259. IPW_PIN_SETRTS,
  260. 0,
  261. NULL,
  262. 0,
  263. 200000);
  264. if (result < 0)
  265. dev_err(&port->dev, "setting dtr failed (error = %d)", result);
  266. kfree(buf_flow_init);
  267. return 0;
  268. }
  269. static void ipw_close(struct usb_serial_port *port, struct file * filp)
  270. {
  271. struct usb_device *dev = port->serial->dev;
  272. int result;
  273. if (tty_hung_up_p(filp)) {
  274. dbg("%s: tty_hung_up_p ...", __FUNCTION__);
  275. return;
  276. }
  277. /*--1: drop the dtr */
  278. dbg("%s:dropping dtr",__FUNCTION__);
  279. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  280. IPW_SIO_SET_PIN,
  281. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  282. IPW_PIN_CLRDTR,
  283. 0,
  284. NULL,
  285. 0,
  286. 200000);
  287. if (result < 0)
  288. dev_err(&port->dev, "dropping dtr failed (error = %d)", result);
  289. /*--2: drop the rts */
  290. dbg("%s:dropping rts",__FUNCTION__);
  291. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  292. IPW_SIO_SET_PIN, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  293. IPW_PIN_CLRRTS,
  294. 0,
  295. NULL,
  296. 0,
  297. 200000);
  298. if (result < 0)
  299. dev_err(&port->dev, "dropping rts failed (error = %d)", result);
  300. /*--3: purge */
  301. dbg("%s:sending purge",__FUNCTION__);
  302. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  303. IPW_SIO_PURGE, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  304. 0x03,
  305. 0,
  306. NULL,
  307. 0,
  308. 200000);
  309. if (result < 0)
  310. dev_err(&port->dev, "purge failed (error = %d)", result);
  311. /* send RXBULK_off (tell modem to stop transmitting bulk data on rx chan) */
  312. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  313. IPW_SIO_RXCTL,
  314. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  315. IPW_RXBULK_OFF,
  316. 0, /* index */
  317. NULL,
  318. 0,
  319. 100000);
  320. if (result < 0)
  321. dev_err(&port->dev, "Disabling bulk RxRead failed (error = %d)", result);
  322. /* shutdown any in-flight urbs that we know about */
  323. usb_kill_urb(port->read_urb);
  324. usb_kill_urb(port->write_urb);
  325. }
  326. static void ipw_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
  327. {
  328. struct usb_serial_port *port = urb->context;
  329. dbg("%s", __FUNCTION__);
  330. if (urb->status)
  331. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  332. schedule_work(&port->work);
  333. }
  334. static int ipw_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  335. {
  336. struct usb_device *dev = port->serial->dev;
  337. int ret;
  338. dbg("%s: TOP: count=%d, in_interrupt=%ld", __FUNCTION__,
  339. count, in_interrupt() );
  340. if (count == 0) {
  341. dbg("%s - write request of 0 bytes", __FUNCTION__);
  342. return 0;
  343. }
  344. spin_lock(&port->lock);
  345. if (port->write_urb_busy) {
  346. spin_unlock(&port->lock);
  347. dbg("%s - already writing", __FUNCTION__);
  348. return 0;
  349. }
  350. port->write_urb_busy = 1;
  351. spin_unlock(&port->lock);
  352. count = min(count, port->bulk_out_size);
  353. memcpy(port->bulk_out_buffer, buf, count);
  354. dbg("%s count now:%d", __FUNCTION__, count);
  355. usb_fill_bulk_urb(port->write_urb, dev,
  356. usb_sndbulkpipe(dev, port->bulk_out_endpointAddress),
  357. port->write_urb->transfer_buffer,
  358. count,
  359. ipw_write_bulk_callback,
  360. port);
  361. ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  362. if (ret != 0) {
  363. port->write_urb_busy = 0;
  364. dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, ret);
  365. return ret;
  366. }
  367. dbg("%s returning %d", __FUNCTION__, count);
  368. return count;
  369. }
  370. static int ipw_probe(struct usb_serial_port *port)
  371. {
  372. return 0;
  373. }
  374. static int ipw_disconnect(struct usb_serial_port *port)
  375. {
  376. usb_set_serial_port_data(port, NULL);
  377. return 0;
  378. }
  379. static struct usb_serial_device_type ipw_device = {
  380. .owner = THIS_MODULE,
  381. .name = "IPWireless converter",
  382. .short_name = "ipw",
  383. .id_table = usb_ipw_ids,
  384. .num_interrupt_in = NUM_DONT_CARE,
  385. .num_bulk_in = 1,
  386. .num_bulk_out = 1,
  387. .num_ports = 1,
  388. .open = ipw_open,
  389. .close = ipw_close,
  390. .port_probe = ipw_probe,
  391. .port_remove = ipw_disconnect,
  392. .write = ipw_write,
  393. .write_bulk_callback = ipw_write_bulk_callback,
  394. .read_bulk_callback = ipw_read_bulk_callback,
  395. };
  396. static int usb_ipw_init(void)
  397. {
  398. int retval;
  399. retval = usb_serial_register(&ipw_device);
  400. if (retval)
  401. return retval;
  402. retval = usb_register(&usb_ipw_driver);
  403. if (retval) {
  404. usb_serial_deregister(&ipw_device);
  405. return retval;
  406. }
  407. info(DRIVER_DESC " " DRIVER_VERSION);
  408. return 0;
  409. }
  410. static void usb_ipw_exit(void)
  411. {
  412. usb_deregister(&usb_ipw_driver);
  413. usb_serial_deregister(&ipw_device);
  414. }
  415. module_init(usb_ipw_init);
  416. module_exit(usb_ipw_exit);
  417. /* Module information */
  418. MODULE_AUTHOR( DRIVER_AUTHOR );
  419. MODULE_DESCRIPTION( DRIVER_DESC );
  420. MODULE_LICENSE("GPL");
  421. module_param(debug, bool, S_IRUGO | S_IWUSR);
  422. MODULE_PARM_DESC(debug, "Debug enabled or not");