visor.c 31 KB

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