visor.c 32 KB

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