visor.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this driver
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/usb.h>
  28. #include "usb-serial.h"
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open (struct usb_serial_port *port, struct file *filp);
  37. static void visor_close (struct usb_serial_port *port, struct file *filp);
  38. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  39. static int visor_write_room (struct usb_serial_port *port);
  40. static int visor_chars_in_buffer (struct usb_serial_port *port);
  41. static void visor_throttle (struct usb_serial_port *port);
  42. static void visor_unthrottle (struct usb_serial_port *port);
  43. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id);
  44. static int visor_calc_num_ports(struct usb_serial *serial);
  45. static void visor_shutdown (struct usb_serial *serial);
  46. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  47. static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios);
  48. static void visor_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  49. static void visor_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  50. static void visor_read_int_callback (struct urb *urb, struct pt_regs *regs);
  51. static int clie_3_5_startup (struct usb_serial *serial);
  52. static int treo_attach (struct usb_serial *serial);
  53. static int clie_5_attach (struct usb_serial *serial);
  54. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id);
  55. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id);
  56. /* Parameters that may be passed into the module. */
  57. static int debug;
  58. static __u16 vendor;
  59. static __u16 product;
  60. static struct usb_device_id id_table [] = {
  61. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  62. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  63. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  64. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  65. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  66. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  67. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  68. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  69. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  70. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  71. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  72. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  73. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  74. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  75. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  76. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  77. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  78. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  79. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  80. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  81. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  82. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  83. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  84. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  85. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  86. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  87. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID),
  88. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  89. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  90. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  91. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  92. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  93. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  94. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  95. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  96. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  97. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  98. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  99. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  100. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  101. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  102. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  103. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  104. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  105. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  106. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  107. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  108. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  109. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  110. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  111. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  112. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  113. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  114. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  115. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  116. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  117. { }, /* optional parameter entry */
  118. { } /* Terminating entry */
  119. };
  120. static struct usb_device_id clie_id_5_table [] = {
  121. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  122. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  123. { }, /* optional parameter entry */
  124. { } /* Terminating entry */
  125. };
  126. static struct usb_device_id clie_id_3_5_table [] = {
  127. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  128. { } /* Terminating entry */
  129. };
  130. static struct usb_device_id id_table_combined [] = {
  131. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  132. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  133. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  134. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  135. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  136. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  137. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  146. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  147. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  148. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  154. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  155. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  156. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  157. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  158. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  159. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  160. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  161. { }, /* optional parameter entry */
  162. { } /* Terminating entry */
  163. };
  164. MODULE_DEVICE_TABLE (usb, id_table_combined);
  165. static struct usb_driver visor_driver = {
  166. .owner = THIS_MODULE,
  167. .name = "visor",
  168. .probe = usb_serial_probe,
  169. .disconnect = usb_serial_disconnect,
  170. .id_table = id_table_combined,
  171. .no_dynamic_id = 1,
  172. };
  173. /* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
  174. static struct usb_serial_driver handspring_device = {
  175. .driver = {
  176. .owner = THIS_MODULE,
  177. .name = "visor",
  178. },
  179. .description = "Handspring Visor / Palm OS",
  180. .id_table = id_table,
  181. .num_interrupt_in = NUM_DONT_CARE,
  182. .num_bulk_in = 2,
  183. .num_bulk_out = 2,
  184. .num_ports = 2,
  185. .open = visor_open,
  186. .close = visor_close,
  187. .throttle = visor_throttle,
  188. .unthrottle = visor_unthrottle,
  189. .attach = treo_attach,
  190. .probe = visor_probe,
  191. .calc_num_ports = visor_calc_num_ports,
  192. .shutdown = visor_shutdown,
  193. .ioctl = visor_ioctl,
  194. .set_termios = visor_set_termios,
  195. .write = visor_write,
  196. .write_room = visor_write_room,
  197. .chars_in_buffer = visor_chars_in_buffer,
  198. .write_bulk_callback = visor_write_bulk_callback,
  199. .read_bulk_callback = visor_read_bulk_callback,
  200. .read_int_callback = visor_read_int_callback,
  201. };
  202. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  203. static struct usb_serial_driver clie_5_device = {
  204. .driver = {
  205. .owner = THIS_MODULE,
  206. .name = "clie_5",
  207. },
  208. .description = "Sony Clie 5.0",
  209. .id_table = clie_id_5_table,
  210. .num_interrupt_in = NUM_DONT_CARE,
  211. .num_bulk_in = 2,
  212. .num_bulk_out = 2,
  213. .num_ports = 2,
  214. .open = visor_open,
  215. .close = visor_close,
  216. .throttle = visor_throttle,
  217. .unthrottle = visor_unthrottle,
  218. .attach = clie_5_attach,
  219. .probe = visor_probe,
  220. .calc_num_ports = visor_calc_num_ports,
  221. .shutdown = visor_shutdown,
  222. .ioctl = visor_ioctl,
  223. .set_termios = visor_set_termios,
  224. .write = visor_write,
  225. .write_room = visor_write_room,
  226. .chars_in_buffer = visor_chars_in_buffer,
  227. .write_bulk_callback = visor_write_bulk_callback,
  228. .read_bulk_callback = visor_read_bulk_callback,
  229. .read_int_callback = visor_read_int_callback,
  230. };
  231. /* device info for the Sony Clie OS version 3.5 */
  232. static struct usb_serial_driver clie_3_5_device = {
  233. .driver = {
  234. .owner = THIS_MODULE,
  235. .name = "clie_3.5",
  236. },
  237. .description = "Sony Clie 3.5",
  238. .id_table = clie_id_3_5_table,
  239. .num_interrupt_in = 0,
  240. .num_bulk_in = 1,
  241. .num_bulk_out = 1,
  242. .num_ports = 1,
  243. .open = visor_open,
  244. .close = visor_close,
  245. .throttle = visor_throttle,
  246. .unthrottle = visor_unthrottle,
  247. .attach = clie_3_5_startup,
  248. .ioctl = visor_ioctl,
  249. .set_termios = visor_set_termios,
  250. .write = visor_write,
  251. .write_room = visor_write_room,
  252. .chars_in_buffer = visor_chars_in_buffer,
  253. .write_bulk_callback = visor_write_bulk_callback,
  254. .read_bulk_callback = visor_read_bulk_callback,
  255. };
  256. struct visor_private {
  257. spinlock_t lock;
  258. int bytes_in;
  259. int bytes_out;
  260. int outstanding_urbs;
  261. int throttled;
  262. };
  263. /* number of outstanding urbs to prevent userspace DoS from happening */
  264. #define URB_UPPER_LIMIT 42
  265. static int stats;
  266. /******************************************************************************
  267. * Handspring Visor specific driver functions
  268. ******************************************************************************/
  269. static int visor_open (struct usb_serial_port *port, struct file *filp)
  270. {
  271. struct usb_serial *serial = port->serial;
  272. struct visor_private *priv = usb_get_serial_port_data(port);
  273. unsigned long flags;
  274. int result = 0;
  275. dbg("%s - port %d", __FUNCTION__, port->number);
  276. if (!port->read_urb) {
  277. /* this is needed for some brain dead Sony devices */
  278. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  279. return -ENODEV;
  280. }
  281. spin_lock_irqsave(&priv->lock, flags);
  282. priv->bytes_in = 0;
  283. priv->bytes_out = 0;
  284. priv->outstanding_urbs = 0;
  285. priv->throttled = 0;
  286. spin_unlock_irqrestore(&priv->lock, flags);
  287. /*
  288. * Force low_latency on so that our tty_push actually forces the data
  289. * through, otherwise it is scheduled, and with high data rates (like
  290. * with OHCI) data can get lost.
  291. */
  292. if (port->tty)
  293. port->tty->low_latency = 1;
  294. /* Start reading from the device */
  295. usb_fill_bulk_urb (port->read_urb, serial->dev,
  296. usb_rcvbulkpipe (serial->dev,
  297. port->bulk_in_endpointAddress),
  298. port->read_urb->transfer_buffer,
  299. port->read_urb->transfer_buffer_length,
  300. visor_read_bulk_callback, port);
  301. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  302. if (result) {
  303. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n",
  304. __FUNCTION__, result);
  305. goto exit;
  306. }
  307. if (port->interrupt_in_urb) {
  308. dbg("%s - adding interrupt input for treo", __FUNCTION__);
  309. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  310. if (result)
  311. dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n",
  312. __FUNCTION__, result);
  313. }
  314. exit:
  315. return result;
  316. }
  317. static void visor_close (struct usb_serial_port *port, struct file * filp)
  318. {
  319. struct visor_private *priv = usb_get_serial_port_data(port);
  320. unsigned char *transfer_buffer;
  321. dbg("%s - port %d", __FUNCTION__, port->number);
  322. /* shutdown our urbs */
  323. usb_kill_urb(port->read_urb);
  324. if (port->interrupt_in_urb)
  325. usb_kill_urb(port->interrupt_in_urb);
  326. /* Try to send shutdown message, if the device is gone, this will just fail. */
  327. transfer_buffer = kmalloc (0x12, GFP_KERNEL);
  328. if (transfer_buffer) {
  329. usb_control_msg (port->serial->dev,
  330. usb_rcvctrlpipe(port->serial->dev, 0),
  331. VISOR_CLOSE_NOTIFICATION, 0xc2,
  332. 0x0000, 0x0000,
  333. transfer_buffer, 0x12, 300);
  334. kfree (transfer_buffer);
  335. }
  336. if (stats)
  337. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  338. priv->bytes_in, priv->bytes_out);
  339. }
  340. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  341. {
  342. struct visor_private *priv = usb_get_serial_port_data(port);
  343. struct usb_serial *serial = port->serial;
  344. struct urb *urb;
  345. unsigned char *buffer;
  346. unsigned long flags;
  347. int status;
  348. dbg("%s - port %d", __FUNCTION__, port->number);
  349. spin_lock_irqsave(&priv->lock, flags);
  350. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  351. spin_unlock_irqrestore(&priv->lock, flags);
  352. dbg("%s - write limit hit\n", __FUNCTION__);
  353. return 0;
  354. }
  355. spin_unlock_irqrestore(&priv->lock, flags);
  356. buffer = kmalloc (count, GFP_ATOMIC);
  357. if (!buffer) {
  358. dev_err(&port->dev, "out of memory\n");
  359. return -ENOMEM;
  360. }
  361. urb = usb_alloc_urb(0, GFP_ATOMIC);
  362. if (!urb) {
  363. dev_err(&port->dev, "no more free urbs\n");
  364. kfree (buffer);
  365. return -ENOMEM;
  366. }
  367. memcpy (buffer, buf, count);
  368. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
  369. usb_fill_bulk_urb (urb, serial->dev,
  370. usb_sndbulkpipe (serial->dev,
  371. port->bulk_out_endpointAddress),
  372. buffer, count,
  373. visor_write_bulk_callback, port);
  374. /* send it down the pipe */
  375. status = usb_submit_urb(urb, GFP_ATOMIC);
  376. if (status) {
  377. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  378. __FUNCTION__, status);
  379. count = status;
  380. kfree (buffer);
  381. } else {
  382. spin_lock_irqsave(&priv->lock, flags);
  383. ++priv->outstanding_urbs;
  384. priv->bytes_out += count;
  385. spin_unlock_irqrestore(&priv->lock, flags);
  386. }
  387. /* we are done with this urb, so let the host driver
  388. * really free it when it is finished with it */
  389. usb_free_urb (urb);
  390. return count;
  391. }
  392. static int visor_write_room (struct usb_serial_port *port)
  393. {
  394. dbg("%s - port %d", __FUNCTION__, port->number);
  395. /*
  396. * We really can take anything the user throws at us
  397. * but let's pick a nice big number to tell the tty
  398. * layer that we have lots of free space
  399. */
  400. return 2048;
  401. }
  402. static int visor_chars_in_buffer (struct usb_serial_port *port)
  403. {
  404. dbg("%s - port %d", __FUNCTION__, port->number);
  405. /*
  406. * We can't really account for how much data we
  407. * have sent out, but hasn't made it through to the
  408. * device, so just tell the tty layer that everything
  409. * is flushed.
  410. */
  411. return 0;
  412. }
  413. static void visor_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  414. {
  415. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  416. struct visor_private *priv = usb_get_serial_port_data(port);
  417. unsigned long flags;
  418. /* free up the transfer buffer, as usb_free_urb() does not do this */
  419. kfree (urb->transfer_buffer);
  420. dbg("%s - port %d", __FUNCTION__, port->number);
  421. if (urb->status)
  422. dbg("%s - nonzero write bulk status received: %d",
  423. __FUNCTION__, urb->status);
  424. spin_lock_irqsave(&priv->lock, flags);
  425. --priv->outstanding_urbs;
  426. spin_unlock_irqrestore(&priv->lock, flags);
  427. schedule_work(&port->work);
  428. }
  429. static void visor_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  430. {
  431. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  432. struct visor_private *priv = usb_get_serial_port_data(port);
  433. unsigned char *data = urb->transfer_buffer;
  434. struct tty_struct *tty;
  435. unsigned long flags;
  436. int i;
  437. int throttled;
  438. int result;
  439. dbg("%s - port %d", __FUNCTION__, port->number);
  440. if (urb->status) {
  441. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  442. return;
  443. }
  444. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  445. tty = port->tty;
  446. if (tty && urb->actual_length) {
  447. for (i = 0; i < urb->actual_length ; ++i) {
  448. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  449. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  450. tty_flip_buffer_push(tty);
  451. }
  452. /* this doesn't actually push the data through unless tty->low_latency is set */
  453. tty_insert_flip_char(tty, data[i], 0);
  454. }
  455. tty_flip_buffer_push(tty);
  456. }
  457. spin_lock_irqsave(&priv->lock, flags);
  458. priv->bytes_in += urb->actual_length;
  459. throttled = priv->throttled;
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. /* Continue trying to always read if we should */
  462. if (!throttled) {
  463. usb_fill_bulk_urb (port->read_urb, port->serial->dev,
  464. usb_rcvbulkpipe(port->serial->dev,
  465. port->bulk_in_endpointAddress),
  466. port->read_urb->transfer_buffer,
  467. port->read_urb->transfer_buffer_length,
  468. visor_read_bulk_callback, port);
  469. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  470. if (result)
  471. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  472. }
  473. return;
  474. }
  475. static void visor_read_int_callback (struct urb *urb, struct pt_regs *regs)
  476. {
  477. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  478. int result;
  479. switch (urb->status) {
  480. case 0:
  481. /* success */
  482. break;
  483. case -ECONNRESET:
  484. case -ENOENT:
  485. case -ESHUTDOWN:
  486. /* this urb is terminated, clean up */
  487. dbg("%s - urb shutting down with status: %d",
  488. __FUNCTION__, urb->status);
  489. return;
  490. default:
  491. dbg("%s - nonzero urb status received: %d",
  492. __FUNCTION__, urb->status);
  493. goto exit;
  494. }
  495. /*
  496. * This information is still unknown what it can be used for.
  497. * If anyone has an idea, please let the author know...
  498. *
  499. * Rumor has it this endpoint is used to notify when data
  500. * is ready to be read from the bulk ones.
  501. */
  502. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  503. urb->actual_length, urb->transfer_buffer);
  504. exit:
  505. result = usb_submit_urb (urb, GFP_ATOMIC);
  506. if (result)
  507. dev_err(&urb->dev->dev, "%s - Error %d submitting interrupt urb\n",
  508. __FUNCTION__, result);
  509. }
  510. static void visor_throttle (struct usb_serial_port *port)
  511. {
  512. struct visor_private *priv = usb_get_serial_port_data(port);
  513. unsigned long flags;
  514. dbg("%s - port %d", __FUNCTION__, port->number);
  515. spin_lock_irqsave(&priv->lock, flags);
  516. priv->throttled = 1;
  517. spin_unlock_irqrestore(&priv->lock, flags);
  518. }
  519. static void visor_unthrottle (struct usb_serial_port *port)
  520. {
  521. struct visor_private *priv = usb_get_serial_port_data(port);
  522. unsigned long flags;
  523. int result;
  524. dbg("%s - port %d", __FUNCTION__, port->number);
  525. spin_lock_irqsave(&priv->lock, flags);
  526. priv->throttled = 0;
  527. spin_unlock_irqrestore(&priv->lock, flags);
  528. port->read_urb->dev = port->serial->dev;
  529. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  530. if (result)
  531. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  532. }
  533. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id)
  534. {
  535. struct device *dev = &serial->dev->dev;
  536. struct visor_connection_info *connection_info;
  537. unsigned char *transfer_buffer;
  538. char *string;
  539. int retval = 0;
  540. int i;
  541. int num_ports = 0;
  542. dbg("%s", __FUNCTION__);
  543. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  544. if (!transfer_buffer) {
  545. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
  546. sizeof(*connection_info));
  547. return -ENOMEM;
  548. }
  549. /* send a get connection info request */
  550. retval = usb_control_msg (serial->dev,
  551. usb_rcvctrlpipe(serial->dev, 0),
  552. VISOR_GET_CONNECTION_INFORMATION,
  553. 0xc2, 0x0000, 0x0000, transfer_buffer,
  554. sizeof(*connection_info), 300);
  555. if (retval < 0) {
  556. dev_err(dev, "%s - error %d getting connection information\n",
  557. __FUNCTION__, retval);
  558. goto exit;
  559. }
  560. if (retval == sizeof(*connection_info)) {
  561. connection_info = (struct visor_connection_info *)transfer_buffer;
  562. num_ports = le16_to_cpu(connection_info->num_ports);
  563. for (i = 0; i < num_ports; ++i) {
  564. switch (connection_info->connections[i].port_function_id) {
  565. case VISOR_FUNCTION_GENERIC:
  566. string = "Generic";
  567. break;
  568. case VISOR_FUNCTION_DEBUGGER:
  569. string = "Debugger";
  570. break;
  571. case VISOR_FUNCTION_HOTSYNC:
  572. string = "HotSync";
  573. break;
  574. case VISOR_FUNCTION_CONSOLE:
  575. string = "Console";
  576. break;
  577. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  578. string = "Remote File System";
  579. break;
  580. default:
  581. string = "unknown";
  582. break;
  583. }
  584. dev_info(dev, "%s: port %d, is for %s use\n",
  585. serial->type->description,
  586. connection_info->connections[i].port, string);
  587. }
  588. }
  589. /*
  590. * Handle devices that report invalid stuff here.
  591. */
  592. if (num_ports == 0 || num_ports > 2) {
  593. dev_warn (dev, "%s: No valid connect info available\n",
  594. serial->type->description);
  595. num_ports = 2;
  596. }
  597. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  598. num_ports);
  599. /*
  600. * save off our num_ports info so that we can use it in the
  601. * calc_num_ports callback
  602. */
  603. usb_set_serial_data(serial, (void *)(long)num_ports);
  604. /* ask for the number of bytes available, but ignore the response as it is broken */
  605. retval = usb_control_msg (serial->dev,
  606. usb_rcvctrlpipe(serial->dev, 0),
  607. VISOR_REQUEST_BYTES_AVAILABLE,
  608. 0xc2, 0x0000, 0x0005, transfer_buffer,
  609. 0x02, 300);
  610. if (retval < 0)
  611. dev_err(dev, "%s - error %d getting bytes available request\n",
  612. __FUNCTION__, retval);
  613. retval = 0;
  614. exit:
  615. kfree (transfer_buffer);
  616. return retval;
  617. }
  618. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id)
  619. {
  620. struct device *dev = &serial->dev->dev;
  621. struct palm_ext_connection_info *connection_info;
  622. unsigned char *transfer_buffer;
  623. int retval;
  624. dbg("%s", __FUNCTION__);
  625. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  626. if (!transfer_buffer) {
  627. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
  628. sizeof(*connection_info));
  629. return -ENOMEM;
  630. }
  631. retval = usb_control_msg (serial->dev,
  632. usb_rcvctrlpipe(serial->dev, 0),
  633. PALM_GET_EXT_CONNECTION_INFORMATION,
  634. 0xc2, 0x0000, 0x0000, transfer_buffer,
  635. sizeof (*connection_info), 300);
  636. if (retval < 0)
  637. dev_err(dev, "%s - error %d getting connection info\n",
  638. __FUNCTION__, retval);
  639. else
  640. usb_serial_debug_data(debug, &serial->dev->dev, __FUNCTION__,
  641. retval, transfer_buffer);
  642. kfree (transfer_buffer);
  643. return 0;
  644. }
  645. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id)
  646. {
  647. int retval = 0;
  648. int (*startup) (struct usb_serial *serial, const struct usb_device_id *id);
  649. dbg("%s", __FUNCTION__);
  650. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  651. err("active config #%d != 1 ??",
  652. serial->dev->actconfig->desc.bConfigurationValue);
  653. return -ENODEV;
  654. }
  655. if (id->driver_info) {
  656. startup = (void *)id->driver_info;
  657. retval = startup(serial, id);
  658. }
  659. return retval;
  660. }
  661. static int visor_calc_num_ports (struct usb_serial *serial)
  662. {
  663. int num_ports = (int)(long)(usb_get_serial_data(serial));
  664. if (num_ports)
  665. usb_set_serial_data(serial, NULL);
  666. return num_ports;
  667. }
  668. static int generic_startup(struct usb_serial *serial)
  669. {
  670. struct visor_private *priv;
  671. int i;
  672. for (i = 0; i < serial->num_ports; ++i) {
  673. priv = kmalloc (sizeof(*priv), GFP_KERNEL);
  674. if (!priv)
  675. return -ENOMEM;
  676. memset (priv, 0x00, sizeof(*priv));
  677. spin_lock_init(&priv->lock);
  678. usb_set_serial_port_data(serial->port[i], priv);
  679. }
  680. return 0;
  681. }
  682. static int clie_3_5_startup (struct usb_serial *serial)
  683. {
  684. struct device *dev = &serial->dev->dev;
  685. int result;
  686. u8 data;
  687. dbg("%s", __FUNCTION__);
  688. /*
  689. * Note that PEG-300 series devices expect the following two calls.
  690. */
  691. /* get the config number */
  692. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  693. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  694. 0, 0, &data, 1, 3000);
  695. if (result < 0) {
  696. dev_err(dev, "%s: get config number failed: %d\n", __FUNCTION__, result);
  697. return result;
  698. }
  699. if (result != 1) {
  700. dev_err(dev, "%s: get config number bad return length: %d\n", __FUNCTION__, result);
  701. return -EIO;
  702. }
  703. /* get the interface number */
  704. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  705. USB_REQ_GET_INTERFACE,
  706. USB_DIR_IN | USB_RECIP_INTERFACE,
  707. 0, 0, &data, 1, 3000);
  708. if (result < 0) {
  709. dev_err(dev, "%s: get interface number failed: %d\n", __FUNCTION__, result);
  710. return result;
  711. }
  712. if (result != 1) {
  713. dev_err(dev, "%s: get interface number bad return length: %d\n", __FUNCTION__, result);
  714. return -EIO;
  715. }
  716. return generic_startup(serial);
  717. }
  718. static int treo_attach (struct usb_serial *serial)
  719. {
  720. struct usb_serial_port *swap_port;
  721. /* Only do this endpoint hack for the Handspring devices with
  722. * interrupt in endpoints, which for now are the Treo devices. */
  723. if (!((le16_to_cpu(serial->dev->descriptor.idVendor) == HANDSPRING_VENDOR_ID) ||
  724. (le16_to_cpu(serial->dev->descriptor.idVendor) == KYOCERA_VENDOR_ID)) ||
  725. (serial->num_interrupt_in == 0))
  726. goto generic_startup;
  727. dbg("%s", __FUNCTION__);
  728. /*
  729. * It appears that Treos and Kyoceras want to use the
  730. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  731. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  732. * Note that swapping the bulk out endpoints would break lots of
  733. * apps that want to communicate on the second port.
  734. */
  735. #define COPY_PORT(dest, src) \
  736. dest->read_urb = src->read_urb; \
  737. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress; \
  738. dest->bulk_in_buffer = src->bulk_in_buffer; \
  739. dest->interrupt_in_urb = src->interrupt_in_urb; \
  740. dest->interrupt_in_endpointAddress = src->interrupt_in_endpointAddress; \
  741. dest->interrupt_in_buffer = src->interrupt_in_buffer;
  742. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  743. if (!swap_port)
  744. return -ENOMEM;
  745. COPY_PORT(swap_port, serial->port[0]);
  746. COPY_PORT(serial->port[0], serial->port[1]);
  747. COPY_PORT(serial->port[1], swap_port);
  748. kfree(swap_port);
  749. generic_startup:
  750. return generic_startup(serial);
  751. }
  752. static int clie_5_attach (struct usb_serial *serial)
  753. {
  754. dbg("%s", __FUNCTION__);
  755. /* TH55 registers 2 ports.
  756. Communication in from the UX50/TH55 uses bulk_in_endpointAddress from port 0
  757. Communication out to the UX50/TH55 uses bulk_out_endpointAddress from port 1
  758. Lets do a quick and dirty mapping
  759. */
  760. /* some sanity check */
  761. if (serial->num_ports < 2)
  762. return -1;
  763. /* port 0 now uses the modified endpoint Address */
  764. serial->port[0]->bulk_out_endpointAddress = serial->port[1]->bulk_out_endpointAddress;
  765. return generic_startup(serial);
  766. }
  767. static void visor_shutdown (struct usb_serial *serial)
  768. {
  769. dbg("%s", __FUNCTION__);
  770. }
  771. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  772. {
  773. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  774. return -ENOIOCTLCMD;
  775. }
  776. /* This function is all nice and good, but we don't change anything based on it :) */
  777. static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  778. {
  779. unsigned int cflag;
  780. dbg("%s - port %d", __FUNCTION__, port->number);
  781. if ((!port->tty) || (!port->tty->termios)) {
  782. dbg("%s - no tty structures", __FUNCTION__);
  783. return;
  784. }
  785. cflag = port->tty->termios->c_cflag;
  786. /* check that they really want us to change something */
  787. if (old_termios) {
  788. if ((cflag == old_termios->c_cflag) &&
  789. (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
  790. dbg("%s - nothing to change...", __FUNCTION__);
  791. return;
  792. }
  793. }
  794. /* get the byte size */
  795. switch (cflag & CSIZE) {
  796. case CS5: dbg("%s - data bits = 5", __FUNCTION__); break;
  797. case CS6: dbg("%s - data bits = 6", __FUNCTION__); break;
  798. case CS7: dbg("%s - data bits = 7", __FUNCTION__); break;
  799. default:
  800. case CS8: dbg("%s - data bits = 8", __FUNCTION__); break;
  801. }
  802. /* determine the parity */
  803. if (cflag & PARENB)
  804. if (cflag & PARODD)
  805. dbg("%s - parity = odd", __FUNCTION__);
  806. else
  807. dbg("%s - parity = even", __FUNCTION__);
  808. else
  809. dbg("%s - parity = none", __FUNCTION__);
  810. /* figure out the stop bits requested */
  811. if (cflag & CSTOPB)
  812. dbg("%s - stop bits = 2", __FUNCTION__);
  813. else
  814. dbg("%s - stop bits = 1", __FUNCTION__);
  815. /* figure out the flow control settings */
  816. if (cflag & CRTSCTS)
  817. dbg("%s - RTS/CTS is enabled", __FUNCTION__);
  818. else
  819. dbg("%s - RTS/CTS is disabled", __FUNCTION__);
  820. /* determine software flow control */
  821. if (I_IXOFF(port->tty))
  822. dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  823. __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
  824. else
  825. dbg("%s - XON/XOFF is disabled", __FUNCTION__);
  826. /* get the baud rate wanted */
  827. dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
  828. return;
  829. }
  830. static int __init visor_init (void)
  831. {
  832. int i, retval;
  833. /* Only if parameters were passed to us */
  834. if ((vendor>0) && (product>0)) {
  835. struct usb_device_id usb_dev_temp[]=
  836. {{USB_DEVICE(vendor, product),
  837. .driver_info = (kernel_ulong_t)&palm_os_4_probe }};
  838. /* Find the last entry in id_table */
  839. for (i=0; ; i++) {
  840. if (id_table[i].idVendor==0) {
  841. id_table[i] = usb_dev_temp[0];
  842. break;
  843. }
  844. }
  845. /* Find the last entry in id_table_combined */
  846. for (i=0; ; i++) {
  847. if (id_table_combined[i].idVendor==0) {
  848. id_table_combined[i] = usb_dev_temp[0];
  849. break;
  850. }
  851. }
  852. info("Untested USB device specified at time of module insertion");
  853. info("Warning: This is not guaranteed to work");
  854. info("Using a newer kernel is preferred to this method");
  855. info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
  856. vendor, product);
  857. }
  858. retval = usb_serial_register(&handspring_device);
  859. if (retval)
  860. goto failed_handspring_register;
  861. retval = usb_serial_register(&clie_3_5_device);
  862. if (retval)
  863. goto failed_clie_3_5_register;
  864. retval = usb_serial_register(&clie_5_device);
  865. if (retval)
  866. goto failed_clie_5_register;
  867. retval = usb_register(&visor_driver);
  868. if (retval)
  869. goto failed_usb_register;
  870. info(DRIVER_DESC);
  871. return 0;
  872. failed_usb_register:
  873. usb_serial_deregister(&clie_5_device);
  874. failed_clie_5_register:
  875. usb_serial_deregister(&clie_3_5_device);
  876. failed_clie_3_5_register:
  877. usb_serial_deregister(&handspring_device);
  878. failed_handspring_register:
  879. return retval;
  880. }
  881. static void __exit visor_exit (void)
  882. {
  883. usb_deregister (&visor_driver);
  884. usb_serial_deregister (&handspring_device);
  885. usb_serial_deregister (&clie_3_5_device);
  886. usb_serial_deregister (&clie_5_device);
  887. }
  888. module_init(visor_init);
  889. module_exit(visor_exit);
  890. MODULE_AUTHOR( DRIVER_AUTHOR );
  891. MODULE_DESCRIPTION( DRIVER_DESC );
  892. MODULE_LICENSE("GPL");
  893. module_param(debug, bool, S_IRUGO | S_IWUSR);
  894. MODULE_PARM_DESC(debug, "Debug enabled or not");
  895. module_param(stats, bool, S_IRUGO | S_IWUSR);
  896. MODULE_PARM_DESC(stats, "Enables statistics or not");
  897. module_param(vendor, ushort, 0);
  898. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  899. module_param(product, ushort, 0);
  900. MODULE_PARM_DESC(product, "User specified product ID");