2014-03-17

This feature encodes Oops messages into a QR barcode that is scannable by

any device with a camera.

If the config option for this feature is enabled, then when an Oops is

in progress, the printk() calls' strings are buffered. When the Oops

finishes, the buffer is compressed, encoded into a QR and then displayed

to frame buffer. The compression is done with zlib from lib/.

Current issues:

* the QR code is sometimes displayed on top of the console text,

sometimes under it, thus making it unscannable

* the compression rate could be better than what zlib offers

* not tested for Oops messages issued from several CPUs

As far as decoding is concerned, there are a lot of apps on mobile devices

that decode QR codes (just text mostly). In order to make this work, an

app which also decodes the QR code is needed. I will be working the next

couple of weeks on an Android app which scans the Oops encoding QR and

sends it to a server which keeps track of these Oopses that are sent to

it making a sort of stream of the latest Oopses. Any thoughts on what the best

workflow would be are more than welcomed.

Also, if there are any suggestions on how to solve some of the issues,

they are more than welcomed.

Signed-off-by: Teodora Baluta <teobaluta@gmail.com>

---

include/linux/print_oops.h | 11 +

include/linux/qrencode.h | 546 +++++++++++++

kernel/Makefile | 1 +

kernel/panic.c | 5 +

kernel/print_oops.c | 173 +++++

kernel/printk/printk.c | 9 +-

lib/Kconfig | 5 +

lib/Kconfig.debug | 11 +

lib/Makefile | 3 +

lib/qr/Makefile | 6 +

lib/qr/bitstream.c | 233 ++++++

lib/qr/bitstream.h | 37 +

lib/qr/mask.c | 320 ++++++++

lib/qr/mask.h | 39 +

lib/qr/mmask.c | 175 +++++

lib/qr/mmask.h | 36 +

lib/qr/mqrspec.c | 259 +++++++

lib/qr/mqrspec.h | 155 ++++

lib/qr/qrencode.c | 871 +++++++++++++++++++++

lib/qr/qrencode.h | 546 +++++++++++++

lib/qr/qrinput.c | 1834 ++++++++++++++++++++++++++++++++++++++++++++

lib/qr/qrinput.h | 129 ++++

lib/qr/qrspec.c | 543 +++++++++++++

lib/qr/qrspec.h | 178 +++++

lib/qr/rscode.c | 325 ++++++++

lib/qr/rscode.h | 38 +

lib/qr/split.c | 331 ++++++++

lib/qr/split.h | 44 ++

28 files changed, 6860 insertions(+), 3 deletions(-)

create mode 100644 include/linux/print_oops.h

create mode 100644 include/linux/qrencode.h

create mode 100644 kernel/print_oops.c

create mode 100644 lib/qr/Makefile

create mode 100644 lib/qr/bitstream.c

create mode 100644 lib/qr/bitstream.h

create mode 100644 lib/qr/mask.c

create mode 100644 lib/qr/mask.h

create mode 100644 lib/qr/mmask.c

create mode 100644 lib/qr/mmask.h

create mode 100644 lib/qr/mqrspec.c

create mode 100644 lib/qr/mqrspec.h

create mode 100644 lib/qr/qrencode.c

create mode 100644 lib/qr/qrencode.h

create mode 100644 lib/qr/qrinput.c

create mode 100644 lib/qr/qrinput.h

create mode 100644 lib/qr/qrspec.c

create mode 100644 lib/qr/qrspec.h

create mode 100644 lib/qr/rscode.c

create mode 100644 lib/qr/rscode.h

create mode 100644 lib/qr/split.c

create mode 100644 lib/qr/split.h

diff --git a/include/linux/print_oops.h b/include/linux/print_oops.h

new file mode 100644

index 0000000..9ebb963

--- /dev/null

+++ b/include/linux/print_oops.h

@@ -0,0 +1,11 @@

+#ifndef _ASM_X86_PRINT_OPS_H

+#define _ASM_X86_PRINT_OPS_H

+

+#include <linux/module.h>

+

+#define QR_BUFSIZE 4096

+

+void qr_append(char *text);

+void print_qr_err(void);

+

+#endif /* _PRINT_OPS_H */

diff --git a/include/linux/qrencode.h b/include/linux/qrencode.h

new file mode 100644

index 0000000..e643274

--- /dev/null

+++ b/include/linux/qrencode.h

@@ -0,0 +1,546 @@

+/**

+ * qrencode - QR Code encoder

+ *

+ * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+/** \mainpage

+ * Libqrencode is a library for encoding data in a QR Code symbol, a kind of 2D

+ * symbology.

+ *

+ * \section encoding Encoding

+ *

+ * There are two methods to encode data: <b>encoding a string/data</b> or

+ * <b>encoding a structured data</b>.

+ *

+ * \subsection encoding-string Encoding a string/data

+ * You can encode a string by calling QRcode_encodeString().

+ * The given string is parsed automatically and encoded. If you want to encode

+ * data that can be represented as a C string style (NUL terminated), you can

+ * simply use this way.

+ *

+ * If the input data contains Kanji (Shift-JIS) characters and you want to

+ * encode them as Kanji in QR Code, you should give QR_MODE_KANJI as a hint.

+ * Otherwise, all of non-alphanumeric characters are encoded as 8 bit data.

+ * If you want to encode a whole string in 8 bit mode, you can use

+ * QRcode_encodeString8bit() instead.

+ *

+ * Please note that a C string can not contain NUL characters. If your data

+ * contains NUL, you must use QRcode_encodeData().

+ *

+ * \subsection encoding-input Encoding a structured data

+ * You can construct a structured input data manually. If the structure of the

+ * input data is known, you can use this way.

+ * At first, create a ::QRinput object by QRinput_new(). Then add input data

+ * to the QRinput object by QRinput_append(). Finally call QRcode_encodeInput()

+ * to encode the QRinput data.

+ * You can reuse the QRinput data again to encode it in other symbols with

+ * different parameters.

+ *

+ * \section result Result

+ * The encoded symbol is resulted as a ::QRcode object. It will contain

+ * its version number, width of the symbol and an array represents the symbol.

+ * See ::QRcode for the details. You can free the object by QRcode_free().

+ *

+ * Please note that the version of the result may be larger than specified.

+ * In such cases, the input data would be too large to be encoded in a

+ * symbol of the specified version.

+ *

+ * \section structured Structured append

+ * Libqrencode can generate "Structured-appended" symbols that enables to split

+ * a large data set into mulitple QR codes. A QR code reader concatenates

+ * multiple QR code symbols into a string.

+ * Just like QRcode_encodeString(), you can use QRcode_encodeStringStructured()

+ * to generate structured-appended symbols. This functions returns an instance

+ * of ::QRcode_List. The returned list is a singly-linked list of QRcode: you

+ * can retrieve each QR code in this way:

+ *

+ * \code

+ * QRcode_List *qrcodes;

+ * QRcode_List *entry;

+ * QRcode *qrcode;

+ *

+ * qrcodes = QRcode_encodeStringStructured(...);

+ * entry = qrcodes;

+ * while(entry != NULL) {

+ * qrcode = entry->code;

+ * // do something

+ * entry = entry->next;

+ * }

+ * QRcode_List_free(entry);

+ * \endcode

+ *

+ * Instead of using auto-parsing functions, you can construct your own

+ * structured input. At first, instantiate an object of ::QRinput_Struct

+ * by calling QRinput_Struct_new(). This object can hold multiple ::QRinput,

+ * and one QR code is generated for a ::QRinput.

+ * QRinput_Struct_appendInput() appends a ::QRinput to a ::QRinput_Struct

+ * object. In order to generate structured-appended symbols, it is required to

+ * embed headers to each symbol. You can use

+ * QRinput_Struct_insertStructuredAppendHeaders() to insert appropriate

+ * headers to each symbol. You should call this function just once before

+ * encoding symbols.

+ */

+

+#ifndef __QRENCODE_H__

+#define __QRENCODE_H__

+

+#if defined(__cplusplus)

+extern "C" {

+#endif

+

+/**

+ * Encoding mode.

+ */

+enum QRencodeMode {

+ QR_MODE_NUL = -1, /* Terminator (NUL character). Internal use only */

+ QR_MODE_NUM = 0, /* Numeric mode */

+ QR_MODE_AN, /* Alphabet-numeric mode */

+ QR_MODE_8, /* 8-bit data mode */

+ QR_MODE_KANJI, /* Kanji (shift-jis) mode */

+ QR_MODE_STRUCTURE, /* Internal use only */

+ QR_MODE_ECI, /* ECI mode */

+ QR_MODE_FNC1FIRST, /* FNC1, first position */

+ QR_MODE_FNC1SECOND, /* FNC1, second position */

+};

+

+/**

+ * Level of error correction.

+ */

+enum QRecLevel {

+ QR_ECLEVEL_L = 0, /* lowest */

+ QR_ECLEVEL_M,

+ QR_ECLEVEL_Q,

+ QR_ECLEVEL_H /* highest */

+};

+

+/**

+ * Maximum version (size) of QR-code symbol.

+ */

+#define QRSPEC_VERSION_MAX 40

+

+/**

+ * Maximum version (size) of QR-code symbol.

+ */

+#define MQRSPEC_VERSION_MAX 4

+

+

+/******************************************************************************

+ * Input data (qrinput.c)

+ *****************************************************************************/

+/**

+ * Instantiate an input data object. The version is set to 0 (auto-select)

+ * and the error correction level is set to QR_ECLEVEL_L.

+ * @return an input object (initialized). On error, NULL is returned and errno

+ * is set to indicate the error.

+ * @throw ENOMEM unable to allocate memory.

+ */

+extern struct QRinput *QRinput_new(void);

+

+/**

+ * Instantiate an input data object.

+ * @param version version number.

+ * @param level Error correction level.

+ * @return an input object (initialized). On error, NULL is returned and errno

+ * is set to indicate the error.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ * @throw EINVAL invalid arguments.

+ */

+extern struct QRinput *QRinput_new2(int version, enum QRecLevel level);

+

+/**

+ * Instantiate an input data object. Object's Micro QR Code flag is set.

+ * Unlike with full-sized QR Code, version number must be specified (>0).

+ * @param version version number (1--4).

+ * @param level Error correction level.

+ * @return an input object (initialized). On error, NULL is returned and errno

+ * is set to indicate the error.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ * @throw EINVAL invalid arguments.

+ */

+extern struct QRinput *QRinput_newMQR(int version, enum QRecLevel level);

+

+/**

+ * Append data to an input object.

+ * The data is copied and appended to the input object.

+ * @param input input object.

+ * @param mode encoding mode.

+ * @param size size of data (byte).

+ * @param data a pointer to the memory area of the input data.

+ * @retval 0 success.

+ * @retval -1 an error occurred and errno is set to indeicate the error.

+ * See Execptions for the details.

+ * @throw ENOMEM unable to allocate memory.

+ * @throw EINVAL input data is invalid.

+ *

+ */

+extern int QRinput_append(struct QRinput *input, enum QRencodeMode mode,

+ int size, const unsigned char *data);

+

+/**

+ * Append ECI header.

+ * @param input input object.

+ * @param ecinum ECI indicator number (0 - 999999)

+ * @retval 0 success.

+ * @retval -1 an error occurred and errno is set to indeicate the error.

+ * See Execptions for the details.

+ * @throw ENOMEM unable to allocate memory.

+ * @throw EINVAL input data is invalid.

+ *

+ */

+extern int QRinput_appendECIheader(struct QRinput *input, unsigned int ecinum);

+

+/**

+ * Get current version.

+ * @param input input object.

+ * @return current version.

+ */

+extern int QRinput_getVersion(struct QRinput *input);

+

+/**

+ * Set version of the QR code that is to be encoded.

+ * This function cannot be applied to Micro QR Code.

+ * @param input input object.

+ * @param version version number (0 = auto)

+ * @retval 0 success.

+ * @retval -1 invalid argument.

+ */

+extern int QRinput_setVersion(struct QRinput *input, int version);

+

+/**

+ * Get current error correction level.

+ * @param input input object.

+ * @return Current error correcntion level.

+ */

+extern enum QRecLevel QRinput_getErrorCorrectionLevel(struct QRinput *input);

+

+/**

+ * Set error correction level of the QR code that is to be encoded.

+ * This function cannot be applied to Micro QR Code.

+ * @param input input object.

+ * @param level Error correction level.

+ * @retval 0 success.

+ * @retval -1 invalid argument.

+ */

+extern int QRinput_setErrorCorrectionLevel(struct QRinput *input,

+ enum QRecLevel level);

+

+/**

+ * Set version and error correction level of the QR code at once.

+ * This function is recommened for Micro QR Code.

+ * @param input input object.

+ * @param version version number (0 = auto)

+ * @param level Error correction level.

+ * @retval 0 success.

+ * @retval -1 invalid argument.

+ */

+extern int

+QRinput_setVersionAndErrorCorrectionLevel(struct QRinput *input, int version,

+ enum QRecLevel level);

+

+/**

+ * Free the input object.

+ * All of data chunks in the input object are freed too.

+ * @param input input object.

+ */

+extern void QRinput_free(struct QRinput *input);

+

+/**

+ * Validate the input data.

+ * @param mode encoding mode.

+ * @param size size of data (byte).

+ * @param data a pointer to the memory area of the input data.

+ * @retval 0 success.

+ * @retval -1 invalid arguments.

+ */

+extern int QRinput_check(enum QRencodeMode mode, int size,

+ const unsigned char *data);

+

+/**

+ * Instantiate a set of input data object.

+ * @return an instance of QRinput_Struct. On error, NULL is returned and errno

+ * is set to indicate the error.

+ * @throw ENOMEM unable to allocate memory.

+ */

+extern struct QRinput_Struct *QRinput_Struct_new(void);

+

+/**

+ * Set parity of structured symbols.

+ * @param s structured input object.

+ * @param parity parity of s.

+ */

+extern void QRinput_Struct_setParity(struct QRinput_Struct *s,

+ unsigned char parity);

+

+/**

+ * Append a QRinput object to the set. QRinput created by QRinput_newMQR()

+ * will be rejected.

+ * @warning never append the same QRinput object twice or more.

+ * @param s structured input object.

+ * @param input an input object.

+ * @retval >0 number of input objects in the structure.

+ * @retval -1 an error occurred. See Exceptions for the details.

+ * @throw ENOMEM unable to allocate memory.

+ * @throw EINVAL invalid arguments.

+ */

+extern int QRinput_Struct_appendInput(struct QRinput_Struct *s,

+ struct QRinput *input);

+

+/**

+ * Free all of QRinput in the set.

+ * @param s a structured input object.

+ */

+extern void QRinput_Struct_free(struct QRinput_Struct *s);

+

+/**

+ * Split a QRinput to QRinput_Struct. It calculates a parity, set it, then

+ * insert structured-append headers. QRinput created by QRinput_newMQR() will

+ * be rejected.

+ * @param input input object. Version number and error correction level must be

+ * set.

+ * @return a set of input data. On error, NULL is returned, and errno is set

+ * to indicate the error. See Exceptions for the details.

+ * @throw ERANGE input data is too large.

+ * @throw EINVAL invalid input data.

+ * @throw ENOMEM unable to allocate memory.

+ */

+extern struct QRinput_Struct

+*QRinput_splitQRinputToStruct(struct QRinput *input);

+

+/**

+ * Insert structured-append headers to the input structure. It calculates

+ * a parity and set it if the parity is not set yet.

+ * @param s input structure

+ * @retval 0 success.

+ * @retval -1 an error occurred and errno is set to indeicate the error.

+ * See Execptions for the details.

+ * @throw EINVAL invalid input object.

+ * @throw ENOMEM unable to allocate memory.

+ */

+extern int

+QRinput_Struct_insertStructuredAppendHeaders(struct QRinput_Struct *s);

+

+/**

+ * Set FNC1-1st position flag.

+ */

+extern int QRinput_setFNC1First(struct QRinput *input);

+

+/**

+ * Set FNC1-2nd position flag and application identifier.

+ */

+extern int QRinput_setFNC1Second(struct QRinput *input, unsigned char appid);

+

+/******************************************************************************

+ * QRcode output (qrencode.c)

+ *****************************************************************************/

+

+/**

+ * QRcode class.

+ * Symbol data is represented as an array contains width*width uchars.

+ * Each uchar represents a module (dot). If the less significant bit of

+ * the uchar is 1, the corresponding module is black. The other bits are

+ * meaningless for usual applications, but here its specification is described.

+ *

+ * <pre>

+ * MSB 76543210 LSB

+ * |||||||`- 1=black/0=white

+ * ||||||`-- data and ecc code area

+ * |||||`--- format information

+ * ||||`---- version information

+ * |||`----- timing pattern

+ * ||`------ alignment pattern

+ * |`------- finder pattern and separator

+ * `-------- non-data modules (format, timing, etc.)

+ * </pre>

+ */

+struct QRcode {

+ int version; /* version of the symbol */

+ int width; /* width of the symbol */

+ unsigned char *data; /* symbol data */

+};

+

+/**

+ * Singly-linked list of QRcode. Used to represent a structured symbols.

+ * A list is terminated with NULL.

+ */

+struct QRcode_List {

+ struct QRcode *code;

+ struct QRcode_List *next;

+};

+

+/**

+ * Create a symbol from the input data.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ * @param input input data.

+ * @return an instance of QRcode class. The version of the result QRcode may

+ * be larger than the designated version. On error, NULL is returned,

+ * and errno is set to indicate the error. See Exceptions for the

+ * details.

+ * @throw EINVAL invalid input object.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ */

+extern struct QRcode *QRcode_encodeInput(struct QRinput *input);

+

+/**

+ * Create a symbol from the string. The library automatically parses the input

+ * string and encodes in a QR Code symbol.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ * @param string input string. It must be NUL terminated.

+ * @param version version of the symbol. If 0, the library chooses the minimum

+ * version for the given input data.

+ * @param level error correction level.

+ * @param hint tell the library how Japanese Kanji characters should be

+ * encoded. If QR_MODE_KANJI is given, the library assumes that the

+ * given string contains Shift-JIS characters and encodes them in

+ * Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical

+ * characters will be encoded as is. If you want to embed UTF-8

+ * string, choose this. Other mode will cause EINVAL error.

+ * @param casesensitive case-sensitive(1) or not(0).

+ * @return an instance of QRcode class. The version of the result QRcode may

+ * be larger than the designated version. On error, NULL is returned,

+ * and errno is set to indicate the error. See Exceptions for the

+ * details.

+ * @throw EINVAL invalid input object.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ * @throw ERANGE input data is too large.

+ */

+extern struct QRcode

+*QRcode_encodeString(const char *string, int version,

+ enum QRecLevel level, enum QRencodeMode hint,

+ int casesensitive);

+

+/**

+ * Same to QRcode_encodeString(), but encode whole data in 8-bit mode.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ */

+extern struct QRcode

+*QRcode_encodeString8bit(const char *string, int version,

+ enum QRecLevel level);

+

+/**

+ * Micro QR Code version of QRcode_encodeString().

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ */

+extern struct QRcode

+*QRcode_encodeStringMQR(const char *string, int version, enum QRecLevel level,

+ enum QRencodeMode hint, int casesensitive);

+

+/**

+ * Micro QR Code version of QRcode_encodeString8bit().

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ */

+extern struct QRcode

+*QRcode_encodeString8bitMQR(const char *string, int version,

+ enum QRecLevel level);

+

+/**

+ * Encode byte stream (may include '\0') in 8-bit mode.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ * @param size size of the input data.

+ * @param data input data.

+ * @param version version of the symbol. If 0, the library chooses the minimum

+ * version for the given input data.

+ * @param level error correction level.

+ * @throw EINVAL invalid input object.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ * @throw ERANGE input data is too large.

+ */

+extern struct QRcode

+*QRcode_encodeData(int size, const unsigned char *data,

+ int version, enum QRecLevel level);

+

+/**

+ * Micro QR Code version of QRcode_encodeData().

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ */

+extern struct QRcode

+*QRcode_encodeDataMQR(int size, const unsigned char *data,

+ int version, enum QRecLevel level);

+

+/**

+ * Free the instance of QRcode class.

+ * @param qrcode an instance of QRcode class.

+ */

+extern void QRcode_free(struct QRcode *qrcode);

+

+/**

+ * Create structured symbols from the input data.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ * @param s

+ * @return a singly-linked list of QRcode.

+ */

+extern struct QRcode_List

+*QRcode_encodeInputStructured(struct QRinput_Struct *s);

+

+/**

+ * Create structured symbols from the string. The library automatically parses

+ * the input string and encodes in a QR Code symbol.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ * @param string input string. It must be NUL terminated.

+ * @param version version of the symbol.

+ * @param level error correction level.

+ * @param hint tell the library how Japanese Kanji characters should be

+ * encoded. If QR_MODE_KANJI is given, the library assumes that the

+ * given string contains Shift-JIS characters and encodes them in

+ * Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical

+ * characters will be encoded as is. If you want to embed UTF-8

+ * string, choose this. Other mode will cause EINVAL error.

+ * @param casesensitive case-sensitive(1) or not(0).

+ * @return a singly-linked list of QRcode. On error, NULL is returned, and

+ * errno is set to indicate the error. See Exceptions for the details.

+ * @throw EINVAL invalid input object.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ */

+extern struct QRcode_List

+*QRcode_encodeStringStructured(const char *string, int version,

+ enum QRecLevel level, enum QRencodeMode hint,

+ int casesensitive);

+

+/**

+ * Same to QRcode_encodeStringStructured(), but encode whole data in 8-bit mode.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ */

+extern struct QRcode_List

+*QRcode_encodeString8bitStructured(const char *string, int version,

+ enum QRecLevel level);

+

+/**

+ * Create structured symbols from byte stream (may include '\0'). Wholde data

+ * are encoded in 8-bit mode.

+ * @warning This function is THREAD UNSAFE when pthread is disabled.

+ * @param size size of the input data.

+ * @param data input dat.

+ * @param version version of the symbol.

+ * @param level error correction level.

+ * @return a singly-linked list of QRcode. On error, NULL is returned, and

+ * errno is set to indicate the error. See Exceptions for the details.

+ * @throw EINVAL invalid input object.

+ * @throw ENOMEM unable to allocate memory for input objects.

+ */

+extern struct QRcode_List

+*QRcode_encodeDataStructured(int size, const unsigned char *data,

+ int version, enum QRecLevel level);

+

+/**

+ * Return the number of symbols included in a QRcode_List.

+ * @param qrlist a head entry of a QRcode_List.

+ * @return number of symbols in the list.

+ */

+extern int QRcode_List_size(struct QRcode_List *qrlist);

+

+/**

+ * Free the QRcode_List.

+ * @param qrlist a head entry of a QRcode_List.

+ */

+extern void QRcode_List_free(struct QRcode_List *qrlist);

+

+#endif /* __QRENCODE_H__ */

diff --git a/kernel/Makefile b/kernel/Makefile

index bc010ee..297a619 100644

--- a/kernel/Makefile

+++ b/kernel/Makefile

@@ -26,6 +26,7 @@ obj-y += cpu/

obj-y += irq/

obj-y += rcu/

+obj-$(CONFIG_QR_OOPS) += print_oops.o

obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o

obj-$(CONFIG_FREEZER) += freezer.o

obj-$(CONFIG_PROFILING) += profile.o

diff --git a/kernel/panic.c b/kernel/panic.c

index 6d63003..cceec30 100644

--- a/kernel/panic.c

+++ b/kernel/panic.c

@@ -24,6 +24,8 @@

#include <linux/init.h>

#include <linux/nmi.h>

+#include <linux/print_oops.h>

+

#define PANIC_TIMER_STEP 100

#define PANIC_BLINK_SPD 18

@@ -382,6 +384,9 @@ void print_oops_end_marker(void)

init_oops_id();

printk(KERN_WARNING "---[ end trace %016llx ]---\n",

(unsigned long long)oops_id);

+#ifdef CONFIG_QR_OOPS

+ print_qr_err();

+#endif

}

/*

diff --git a/kernel/print_oops.c b/kernel/print_oops.c

new file mode 100644

index 0000000..089e11e

--- /dev/null

+++ b/kernel/print_oops.c

@@ -0,0 +1,173 @@

+/*

+ *

+ * This is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This code is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ *

+ */

+#include <linux/print_oops.h>

+#include <linux/kdebug.h>

+#include <linux/bug.h>

+#include <linux/qrencode.h>

+#include <linux/fb.h>

+#include <linux/zlib.h>

+

+static char qr_buffer[QR_BUFSIZE];

+static int buf_pos;

+

+#define COMPR_LEVEL 6

+

+static DEFINE_MUTEX(compr_mutex);

+static struct z_stream_s stream;

+

+#define QQQ_WHITE 0x0F

+#define QQQ_BLACK 0x00

+

+void qr_append(char *text)

+{

+ while (*text != '\0') {

+ if (buf_pos == QR_BUFSIZE - 1) {

+ qr_buffer[QR_BUFSIZE - 1] = '\0';

+ return;

+ }

+ qr_buffer[buf_pos] = *text;

+ buf_pos++;

+ text++;

+ }

+}

+

+static inline int compute_w(struct fb_info *info, int qrw)

+{

+ int xres = info->var.xres;

+ int yres = info->var.yres;

+ int minxy = (xres < yres) ? xres : yres;

+ return minxy / qrw / 3;

+}

+

+static int __init qr_compr_init(void)

+{

+ size_t size = max(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),

+ zlib_inflate_workspacesize());

+ stream.workspace = vmalloc(size);

+ if (!stream.workspace)

+ return -ENOMEM;

+ return 0;

+}

+

+static void qr_compr_exit(void)

+{

+ vfree(stream.workspace);

+}

+

+static int qr_compress(void *in, void *out, size_t inlen, size_t outlen)

+{

+ int err, ret;

+

+ ret = -EIO;

+

+ err = qr_compr_init();

+ if (err != 0)

+ goto error;

+ mutex_lock(&compr_mutex);

+ err = zlib_deflateInit(&stream, COMPR_LEVEL);

+ if (err != Z_OK)

+ goto error;

+

+ stream.next_in = in;

+ stream.avail_in = inlen;

+ stream.total_in = 0;

+ stream.next_out = out;

+ stream.avail_out = outlen;

+ stream.total_out = 0;

+

+ err = zlib_deflate(&stream, Z_FINISH);

+ if (err != Z_STREAM_END)

+ goto error;

+

+ err = zlib_deflateEnd(&stream);

+ if (err != Z_OK)

+ goto error;

+

+ if (stream.total_out >= stream.total_in)

+ goto error;

+

+ ret = stream.total_out;

+error:

+ mutex_unlock(&compr_mutex);

+ return ret;

+}

+

+void print_qr_err(void)

+{

+ ssize_t compr_len;

+ struct fb_info *info;

+ struct fb_fillrect rect;

+ struct QRcode *qr;

+

+ int i, j;

+ int w;

+ int is_black;

+

+ char compr_qr_buffer[buf_pos];

+ compr_len = qr_compress(qr_buffer, compr_qr_buffer, buf_pos, buf_pos);

+

+ if (compr_len < 0)

+ return;

+

+

+ qr = QRcode_encodeData(compr_len, compr_qr_buffer, 0, QR_ECLEVEL_H);

+

+ info = registered_fb[0];

+ w = compute_w(info, qr->width);

+

+ rect.width = w;

+ rect.height = w;

+ rect.rop = 0;

+

+ /* Print borders: */

+ rect.color = QQQ_WHITE;

+ for (i = 0; i < qr->width + 2; i++) {

+ /* Top */

+ rect.dx = 0;

+ rect.dy = i * w;

+ cfb_fillrect(info, &rect);

+

+ /* Bottom */

+ rect.dx = (qr->width + 1) * w;

+ rect.dy = i * w;

+ cfb_fillrect(info, &rect);

+

+ /* Left */

+ rect.dx = i * w;

+ rect.dy = 0;

+ cfb_fillrect(info, &rect);

+

+ /* Right */

+ rect.dx = i * w;

+ rect.dy = (qr->width + 1) * w;

+ cfb_fillrect(info, &rect);

+ }

+

+ /* Print actual QR matrix: */

+ for (i = 0; i < qr->width; i++) {

+ for (j = 0; j < qr->width; j++) {

+ rect.dx = (j + 1) * w;

+ rect.dy = (i + 1) * w;

+ is_black = qr->data[i * qr->width + j] & 1;

+ rect.color = is_black ? QQQ_BLACK : QQQ_WHITE;

+ cfb_fillrect(info, &rect);

+ }

+ }

+

+ QRcode_free(qr);

+ qr_compr_exit();

+ buf_pos = 0;

+}

+

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c

index b1d255f..72dd80a 100644

--- a/kernel/printk/printk.c

+++ b/kernel/printk/printk.c

@@ -45,6 +45,7 @@

#include <linux/poll.h>

#include <linux/irq_work.h>

#include <linux/utsname.h>

+#include <linux/print_oops.h>

#include <asm/uaccess.h>

@@ -1076,7 +1077,6 @@ static int syslog_print_all(char __user *buf, int size, bool clear)

next_seq = log_next_seq;

len = 0;

- prev = 0;

while (len >= 0 && seq < next_seq) {

struct printk_log *msg = log_from_idx(idx);

int textlen;

@@ -1569,6 +1569,11 @@ asmlinkage int vprintk_emit(int facility, int level,

}

}

+#ifdef CONFIG_QR_OOPS

+ if (oops_in_progress)

+ qr_append(text);

+#endif

+

if (level == -1)

level = default_message_loglevel;

@@ -1688,7 +1693,6 @@ asmlinkage int printk(const char *fmt, ...)

va_start(args, fmt);

r = vprintk_emit(0, -1, NULL, 0, fmt, args);

va_end(args);

-

return r;

}

EXPORT_SYMBOL(printk);

@@ -2788,7 +2792,6 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,

next_idx = idx;

l = 0;

- prev = 0;

while (seq < dumper->next_seq) {

struct printk_log *msg = log_from_idx(idx);

diff --git a/lib/Kconfig b/lib/Kconfig

index 991c98b..ef591d6 100644

--- a/lib/Kconfig

+++ b/lib/Kconfig

@@ -435,6 +435,11 @@ config SIGNATURE

Digital signature verification. Currently only RSA is supported.

Implementation is done using GnuPG MPI library

+config QRLIB

+ bool "QR encoding library"

+ help

+ QR encoding library

+

#

# libfdt files, only selected if needed.

#

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug

index a48abea..523136c 100644

--- a/lib/Kconfig.debug

+++ b/lib/Kconfig.debug

@@ -15,6 +15,17 @@ config PRINTK_TIME

The behavior is also controlled by the kernel command line

parameter printk.time=1. See Documentation/kernel-parameters.txt

+config QR_OOPS

+ bool "Display QR barcode for Oops messages for debug"

+ depends on PRINTK && QRLIB

+ help

+ Selecting this option makes printk() calls to accumulate

+ the Oops messages in a buffer, compresses the message

+ and prints the OR to the frame buffer device.

+

+ This is an experimental feature at the moment.

+

+

config DEFAULT_MESSAGE_LOGLEVEL

int "Default message log level (1-7)"

range 1 7

diff --git a/lib/Makefile b/lib/Makefile

index 48140e3..c4dff81 100644

--- a/lib/Makefile

+++ b/lib/Makefile

@@ -39,6 +39,9 @@ CFLAGS_kobject.o += -DDEBUG

CFLAGS_kobject_uevent.o += -DDEBUG

endif

+# QR lib

+obj-$(CONFIG_QRLIB) += qr/

+

obj-$(CONFIG_GENERIC_IOMAP) += iomap.o

obj-$(CONFIG_GENERIC_PCI_IOMAP) += pci_iomap.o

obj-$(CONFIG_HAS_IOMEM) += iomap_copy.o devres.o

diff --git a/lib/qr/Makefile b/lib/qr/Makefile

new file mode 100644

index 0000000..2b90691

--- /dev/null

+++ b/lib/qr/Makefile

@@ -0,0 +1,6 @@

+#

+# QR encoding library

+#

+

+EXTRA_FLAGS = -g

+obj-$(CONFIG_QRLIB) = bitstream.o mask.o mmask.o mqrspec.o qrencode.o qrinput.o qrspec.o rscode.o split.o

diff --git a/lib/qr/bitstream.c b/lib/qr/bitstream.c

new file mode 100644

index 0000000..a2c9fc3

--- /dev/null

+++ b/lib/qr/bitstream.c

@@ -0,0 +1,233 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Binary sequence class.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+#include <linux/kernel.h>

+#include <linux/slab.h>

+#include <linux/types.h>

+#include "bitstream.h"

+

+struct BitStream *BitStream_new(void)

+{

+ struct BitStream *bstream;

+

+ bstream = kmalloc(sizeof(struct BitStream), GFP_ATOMIC);

+ if (bstream == NULL)

+ return NULL;

+

+ bstream->length = 0;

+ bstream->data = NULL;

+

+ return bstream;

+}

+

+static int BitStream_allocate(struct BitStream *bstream, int length)

+{

+ unsigned char *data;

+

+ if (bstream == NULL)

+ return -1;

+

+ data = kmalloc(length, GFP_ATOMIC);

+ if (data == NULL)

+ return -1;

+

+ kfree(bstream->data);

+

+ bstream->length = length;

+ bstream->data = data;

+

+ return 0;

+}

+

+static struct BitStream *BitStream_newFromNum(int bits, unsigned int num)

+{

+ unsigned int mask;

+ int i;

+ unsigned char *p;

+ struct BitStream *bstream;

+

+ bstream = BitStream_new();

+ if (bstream == NULL)

+ return NULL;

+

+ if (BitStream_allocate(bstream, bits)) {

+ BitStream_free(bstream);

+ return NULL;

+ }

+

+ p = bstream->data;

+ mask = 1 << (bits - 1);

+ for (i = 0; i < bits; i++) {

+ if (num & mask)

+ *p = 1;

+ else

+ *p = 0;

+ p++;

+ mask = mask >> 1;

+ }

+

+ return bstream;

+}

+

+static struct BitStream *BitStream_newFromBytes(int size, unsigned char *data)

+{

+ unsigned char mask;

+ int i, j;

+ unsigned char *p;

+ struct BitStream *bstream;

+

+ bstream = BitStream_new();

+ if (bstream == NULL)

+ return NULL;

+

+ if (BitStream_allocate(bstream, size * 8)) {

+ BitStream_free(bstream);

+ return NULL;

+ }

+

+ p = bstream->data;

+ for (i = 0; i < size; i++) {

+ mask = 0x80;

+ for (j = 0; j < 8; j++) {

+ if (data & mask)

+ *p = 1;

+ else

+ *p = 0;

+ p++;

+ mask = mask >> 1;

+ }

+ }

+

+ return bstream;

+}

+

+int BitStream_append(struct BitStream *bstream, struct BitStream *arg)

+{

+ unsigned char *data;

+

+ if (arg == NULL)

+ return -1;

+

+ if (arg->length == 0)

+ return 0;

+

+ if (bstream->length == 0) {

+ if (BitStream_allocate(bstream, arg->length))

+ return -1;

+

+ memcpy(bstream->data, arg->data, arg->length);

+ return 0;

+ }

+

+ data = kmalloc(bstream->length + arg->length, GFP_ATOMIC);

+ if (data == NULL)

+ return -1;

+

+ memcpy(data, bstream->data, bstream->length);

+ memcpy(data + bstream->length, arg->data, arg->length);

+

+ kfree(bstream->data);

+ bstream->length += arg->length;

+ bstream->data = data;

+

+ return 0;

+}

+

+int BitStream_appendNum(struct BitStream *bstream, int bits, unsigned int num)

+{

+ struct BitStream *b;

+ int ret;

+

+ if (bits == 0)

+ return 0;

+

+ b = BitStream_newFromNum(bits, num);

+ if (b == NULL)

+ return -1;

+

+ ret = BitStream_append(bstream, b);

+ BitStream_free(b);

+

+ return ret;

+}

+

+int BitStream_appendBytes(struct BitStream *bstream, int size,

+ unsigned char *data)

+{

+ struct BitStream *b;

+ int ret;

+

+ if (size == 0)

+ return 0;

+

+ b = BitStream_newFromBytes(size, data);

+ if (b == NULL)

+ return -1;

+

+ ret = BitStream_append(bstream, b);

+ BitStream_free(b);

+

+ return ret;

+}

+

+unsigned char *BitStream_toByte(struct BitStream *bstream)

+{

+ int i, j, size, bytes;

+ unsigned char *data, v;

+ unsigned char *p;

+

+ size = BitStream_size(bstream);

+ if (size == 0)

+ return NULL;

+

+ data = kmalloc((size + 7) / 8, GFP_ATOMIC);

+ if (data == NULL)

+ return NULL;

+

+ bytes = size / 8;

+

+ p = bstream->data;

+ for (i = 0; i < bytes; i++) {

+ v = 0;

+ for (j = 0; j < 8; j++) {

+ v = v << 1;

+ v |= *p;

+ p++;

+ }

+ data = v;

+ }

+ if (size & 7) {

+ v = 0;

+ for (j = 0; j < (size & 7); j++) {

+ v = v << 1;

+ v |= *p;

+ p++;

+ }

+ data[bytes] = v;

+ }

+

+ return data;

+}

+

+void BitStream_free(struct BitStream *bstream)

+{

+ if (bstream != NULL) {

+ kfree(bstream->data);

+ kfree(bstream);

+ }

+}

diff --git a/lib/qr/bitstream.h b/lib/qr/bitstream.h

new file mode 100644

index 0000000..6a7ea65

--- /dev/null

+++ b/lib/qr/bitstream.h

@@ -0,0 +1,37 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Binary sequence class.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+#ifndef __BITSTREAM_H__

+#define __BITSTREAM_H__

+

+struct BitStream {

+ int length;

+ unsigned char *data;

+};

+

+extern struct BitStream *BitStream_new(void);

+extern int BitStream_append(struct BitStream *bstream, struct BitStream *arg);

+extern int BitStream_appendNum(struct BitStream *bstream, int bits,

+ unsigned int num);

+extern int BitStream_appendBytes(struct BitStream *bstream, int size,

+ unsigned char *data);

+#define BitStream_size(__bstream__) (__bstream__->length)

+extern unsigned char *BitStream_toByte(struct BitStream *bstream);

+extern void BitStream_free(struct BitStream *bstream);

+

+#endif /* __BITSTREAM_H__ */

diff --git a/lib/qr/mask.c b/lib/qr/mask.c

new file mode 100644

index 0000000..b5025cd

--- /dev/null

+++ b/lib/qr/mask.c

@@ -0,0 +1,320 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Masking.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+

+#include <linux/kernel.h>

+#include <linux/slab.h>

+#include <linux/types.h>

+

+#include <linux/qrencode.h>

+#include "qrspec.h"

+#include "mask.h"

+

+static int Mask_writeFormatInformation(int width, unsigned char *frame,

+ int mask, enum QRecLevel level)

+{

+ unsigned int format;

+ unsigned char v;

+ int i;

+ int blacks = 0;

+

+ format = QRspec_getFormatInfo(mask, level);

+

+ for (i = 0; i < 8; i++) {

+ if (format & 1) {

+ blacks += 2;

+ v = 0x85;

+ } else {

+ v = 0x84;

+ }

+ frame[width * 8 + width - 1 - i] = v;

+ if (i < 6)

+ frame[width * i + 8] = v;

+ else

+ frame[width * (i + 1) + 8] = v;

+

+ format = format >> 1;

+ }

+ for (i = 0; i < 7; i++) {

+ if (format & 1) {

+ blacks += 2;

+ v = 0x85;

+ } else

+ v = 0x84;

+

+ frame[width * (width - 7 + i) + 8] = v;

+ if (i == 0)

+ frame[width * 8 + 7] = v;

+ else

+ frame[width * 8 + 6 - i] = v;

+

+ format = format >> 1;

+ }

+

+ return blacks;

+}

+

+/**

+ * Demerit coefficients.

+ * See Section 8.8.2, pp.45, JIS X0510:2004.

+ */

+#define N1 (3)

+#define N2 (3)

+#define N3 (40)

+#define N4 (10)

+

+#define MASKMAKER(__exp__) \

+ int x, y;\

+ int b = 0;\

+\

+ for (y = 0; y < width; y++) {\

+ for (x = 0; x < width; x++) {\

+ if (*s & 0x80) {\

+ *d = *s;\

+ } else {\

+ *d = *s ^ ((__exp__) == 0);\

+ } \

+ b += (int)(*d & 1);\

+ s++; d++;\

+ } \

+ } \

+ return b;

+

+static int Mask_mask0(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER((x + y) & 1)

+}

+

+static int Mask_mask1(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER(y & 1)

+}

+

+static int Mask_mask2(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER(x % 3)

+}

+

+static int Mask_mask3(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER((x + y) % 3)

+}

+

+static int Mask_mask4(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER(((y / 2) + (x / 3)) & 1)

+}

+

+static int Mask_mask5(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER(((x * y) & 1) + (x * y) % 3)

+}

+

+static int Mask_mask6(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER((((x * y) & 1) + (x * y) % 3) & 1)

+}

+

+static int Mask_mask7(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER((((x * y) % 3) + ((x + y) & 1)) & 1)

+}

+

+#define maskNum (8)

+typedef int MaskMaker(int, const unsigned char *, unsigned char *);

+static MaskMaker *maskMakers[maskNum] = {

+ Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3,

+ Mask_mask4, Mask_mask5, Mask_mask6, Mask_mask7

+};

+

+#ifdef WITH_TESTS

+unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame, int mask)

+{

+ unsigned char *masked;

+

+ masked = kmalloc(width * width, GFP_ATOMIC);

+ if (masked == NULL)

+ return NULL;

+

+ maskMakers[mask] (width, frame, masked);

+

+ return masked;

+}

+#endif

+

+unsigned char *Mask_makeMask(int width, unsigned char *frame, int mask,

+ enum QRecLevel level)

+{

+ unsigned char *masked;

+

+ if (mask < 0 || mask >= maskNum)

+ return NULL;

+

+ masked = kmalloc(width * width, GFP_ATOMIC);

+ if (masked == NULL)

+ return NULL;

+

+ maskMakers[mask] (width, frame, masked);

+ Mask_writeFormatInformation(width, masked, mask, level);

+

+ return masked;

+}

+

+static int Mask_calcN1N3(int length, int *runLength)

+{

+ int i;

+ int demerit = 0;

+ int fact;

+

+ for (i = 0; i < length; i++) {

+ if (runLength >= 5)

+ demerit += N1 + (runLength - 5);

+ if ((i & 1)) {

+ if (i >= 3 && i < length - 2

+ && (runLength % 3) == 0) {

+ fact = runLength / 3;

+ if (runLength[i - 2] == fact &&

+ runLength[i - 1] == fact &&

+ runLength[i + 1] == fact &&

+ runLength[i + 2] == fact) {

+ if (i == 3

+ || runLength[i - 3] >= 4 * fact) {

+ demerit += N3;

+ } else if (i + 4 >= length

+ || runLength[i + 3] >=

+ 4 * fact) {

+ demerit += N3;

+ }

+ }

+ }

+ }

+ }

+

+ return demerit;

+}

+

+static int Mask_calcN2(int width, unsigned char *frame)

+{

+ int x, y;

+ unsigned char *p;

+ unsigned char b22, w22;

+ int demerit = 0;

+

+ p = frame + width + 1;

+ for (y = 1; y < width; y++) {

+ for (x = 1; x < width; x++) {

+ b22 = p[0] & p[-1] & p[-width] & p[-width - 1];

+ w22 = p[0] | p[-1] | p[-width] | p[-width - 1];

+ if ((b22 | (w22 ^ 1)) & 1)

+ demerit += N2;

+ p++;

+ }

+ p++;

+ }

+

+ return demerit;

+}

+

+static int Mask_calcRunLength(int width, unsigned char *frame, int dir,

+ int *runLength)

+{

+ int head;

+ int i;

+ unsigned char *p;

+ int pitch;

+

+ pitch = (dir == 0) ? 1 : width;

+ if (frame[0] & 1) {

+ runLength[0] = -1;

+ head = 1;

+ } else {

+ head = 0;

+ }

+ runLength[head] = 1;

+ p = frame + pitch;

+

+ for (i = 1; i < width; i++) {

+ if ((p[0] ^ p[-pitch]) & 1) {

+ head++;

+ runLength[head] = 1;

+ } else {

+ runLength[head]++;

+ }

+ p += pitch;

+ }

+

+ return head + 1;

+}

+

+static int Mask_evaluateSymbol(int width, unsigned char *frame)

+{

+ int x, y;

+ int demerit = 0;

+ int runLength[QRSPEC_WIDTH_MAX + 1];

+ int length;

+

+ demerit += Mask_calcN2(width, frame);

+

+ for (y = 0; y < width; y++) {

+ length =

+ Mask_calcRunLength(width, frame + y * width, 0, runLength);

+ demerit += Mask_calcN1N3(length, runLength);

+ }

+

+ for (x = 0; x < width; x++) {

+ length = Mask_calcRunLength(width, frame + x, 1, runLength);

+ demerit += Mask_calcN1N3(length, runLength);

+ }

+

+ return demerit;

+}

+

+unsigned char *Mask_mask(int width, unsigned char *frame, enum QRecLevel level)

+{

+ int i;

+ unsigned char *mask, *bestMask;

+ int minDemerit = INT_MAX;

+ int blacks;

+ int bratio;

+ int demerit;

+ int w2 = width * width;

+

+ mask = kmalloc(w2, GFP_ATOMIC);

+ if (mask == NULL)

+ return NULL;

+ bestMask = NULL;

+

+ for (i = 0; i < maskNum; i++) {

+ demerit = 0;

+ blacks = maskMakers (width, frame, mask);

+ blacks += Mask_writeFormatInformation(width, mask, i, level);

+ bratio = (200 * blacks + w2) / w2 / 2;

+ demerit = (abs(bratio - 50) / 5) * N4;

+ demerit += Mask_evaluateSymbol(width, mask);

+ if (demerit < minDemerit) {

+ minDemerit = demerit;

+ kfree(bestMask);

+ bestMask = mask;

+ mask = kmalloc(w2, GFP_ATOMIC);

+ if (mask == NULL)

+ break;

+ }

+ }

+ kfree(mask);

+ return bestMask;

+}

diff --git a/lib/qr/mask.h b/lib/qr/mask.h

new file mode 100644

index 0000000..c8e9276

--- /dev/null

+++ b/lib/qr/mask.h

@@ -0,0 +1,39 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Masking.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+#ifndef __MASK_H__

+#define __MASK_H__

+

+extern unsigned char *Mask_makeMask(int width, unsigned char *frame,

+ int mask, enum QRecLevel level);

+extern unsigned char *Mask_mask(int width, unsigned char *frame,

+ enum QRecLevel level);

+

+#ifdef WITH_TESTS

+extern int Mask_calcN2(int width, unsigned char *frame);

+extern int Mask_calcN1N3(int length, int *runLength);

+extern int Mask_calcRunLength(int width, unsigned char *frame,

+ int dir, int *runLength);

+extern int Mask_evaluateSymbol(int width, unsigned char *frame);

+extern int Mask_writeFormatInformation(int width, unsigned char *frame,

+ int mask, enum QRecLevel level);

+extern unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame,

+ int mask);

+#endif

+

+#endif /* __MASK_H__ */

diff --git a/lib/qr/mmask.c b/lib/qr/mmask.c

new file mode 100644

index 0000000..386e12c

--- /dev/null

+++ b/lib/qr/mmask.c

@@ -0,0 +1,175 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Masking for Micro QR Code.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+#include <linux/kernel.h>

+#include <linux/slab.h>

+#include <linux/types.h>

+

+#include <linux/qrencode.h>

+#include "mqrspec.h"

+#include "mmask.h"

+

+static void MMask_writeFormatInformation(int version, int width,

+ unsigned char *frame, int mask,

+ enum QRecLevel level)

+{

+ unsigned int format;

+ unsigned char v;

+ int i;

+

+ format = MQRspec_getFormatInfo(mask, version, level);

+

+ for (i = 0; i < 8; i++) {

+ v = 0x84 | (format & 1);

+ frame[width * (i + 1) + 8] = v;

+ format = format >> 1;

+ }

+ for (i = 0; i < 7; i++) {

+ v = 0x84 | (format & 1);

+ frame[width * 8 + 7 - i] = v;

+ format = format >> 1;

+ }

+}

+

+#define MASKMAKER(__exp__) \

+ int x, y;\

+\

+ for (y = 0; y < width; y++) {\

+ for (x = 0; x < width; x++) {\

+ if (*s & 0x80) {\

+ *d = *s;\

+ } else { \

+ *d = *s ^ ((__exp__) == 0);\

+ } \

+ s++; d++;\

+ } \

+ }

+

+static void Mask_mask0(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER(y & 1)

+}

+

+static void Mask_mask1(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER(((y / 2) + (x / 3)) & 1)

+}

+

+static void Mask_mask2(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER((((x * y) & 1) + (x * y) % 3) & 1)

+}

+

+static void Mask_mask3(int width, const unsigned char *s, unsigned char *d)

+{

+ MASKMAKER((((x + y) & 1) + ((x * y) % 3)) & 1)

+}

+

+#define maskNum (4)

+typedef void MaskMaker(int, const unsigned char *, unsigned char *);

+static MaskMaker *maskMakers[maskNum] = {

+ Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3

+};

+

+#ifdef WITH_TESTS

+unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask)

+{

+ unsigned char *masked;

+

+ masked = kmalloc(width * width, GFP_ATOMIC);

+ if (masked == NULL)

+ return NULL;

+

+ maskMakers[mask] (width, frame, masked);

+

+ return masked;

+}

+#endif

+

+unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask,

+ enum QRecLevel level)

+{

+ unsigned char *masked;

+ int width;

+

+ if (mask < 0 || mask >= maskNum)

+ return NULL;

+

+ width = MQRspec_getWidth(version);

+ masked = kmalloc(width * width, GFP_ATOMIC);

+ if (masked == NULL)

+ return NULL;

+

+ maskMakers[mask] (width, frame, masked);

+ MMask_writeFormatInformation(version, width, masked, mask, level);

+

+ return masked;

+}

+

+static int MMask_evaluateSymbol(int width, unsigned char *frame)

+{

+ int x, y;

+ unsigned char *p;

+ int sum1 = 0, sum2 = 0;

+

+ p = frame + width * (width - 1);

+ for (x = 1; x < width; x++)

+ sum1 += (p[x] & 1);

+

+ p = frame + width * 2 - 1;

+ for (y = 1; y < width; y++) {

+ sum2 += (*p & 1);

+ p += width;

+ }

+

+ return (sum1 <= sum2) ? (sum1 * 16 + sum2) : (sum2 * 16 + sum1);

+}

+

+unsigned char *MMask_mask(int version, unsigned char *frame,

+ enum QRecLevel level)

+{

+ int i;

+ unsigned char *mask, *bestMask;

+ int maxScore = 0;

+ int score;

+ int width;

+

+ width = MQRspec_getWidth(version);

+

+ mask = kmalloc(width * width, GFP_ATOMIC);

+ if (mask == NULL)

+ return NULL;

+ bestMask = NULL;

+

+ for (i = 0; i < maskNum; i++) {

+ score = 0;

+ maskMakers (width, frame, mask);

+ MMask_writeFormatInformation(version, width, mask, i, level);

+ score = MMask_evaluateSymbol(width, mask);

+ if (score > maxScore) {

+ maxScore = score;

+ kfree(bestMask);

+ bestMask = mask;

+ mask = kmalloc(width * width, GFP_ATOMIC);

+ if (mask == NULL)

+ break;

+ }

+ }

+ kfree(mask);

+ return bestMask;

+}

diff --git a/lib/qr/mmask.h b/lib/qr/mmask.h

new file mode 100644

index 0000000..769abf2

--- /dev/null

+++ b/lib/qr/mmask.h

@@ -0,0 +1,36 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Masking for Micro QR Code.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+#ifndef __MMASK_H__

+#define __MMASK_H__

+

+extern unsigned char *MMask_makeMask(int version, unsigned char *frame,

+ int mask, enum QRecLevel level);

+extern unsigned char *MMask_mask(int version, unsigned char *frame,

+ enum QRecLevel level);

+

+#ifdef WITH_TESTS

+extern int MMask_evaluateSymbol(int width, unsigned char *frame);

+extern void MMask_writeFormatInformation(int version, int width,

+ unsigned char *frame, int mask,

+ enum QRecLevel level);

+extern unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame,

+ int mask);

+#endif

+

+#endif /* __MMASK_H__ */

diff --git a/lib/qr/mqrspec.c b/lib/qr/mqrspec.c

new file mode 100644

index 0000000..b5a3408

--- /dev/null

+++ b/lib/qr/mqrspec.c

@@ -0,0 +1,259 @@

+/*

+ * qrencode - QR Code encoder

+ *

+ * Micor QR Code specification in convenient format.

+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>

+ *

+ * This library is free software; you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License as published by

+ * the Free Software Foundation; either version 2 of the License, or

+ * (at your option) any later version.

+ *

+ * This library is distributed in the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warranty of

+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

+ * GNU General Public License for more details.

+ *

+ */

+

+#include <linux/kernel.h>

+#include <linux/slab.h>

+#include <linux/types.h>

+

+#include "mqrspec.h"

+

+/******************************************************************************

+ * Version and capacity

+ *****************************************************************************/

+

+struct MQRspec_Capacity {

+ int width; /* Edge length of the symbol */

+ int ec[4]; /* Number of ECC code (bytes) */

+};

+

+/**

+ * Table of the capacity of symbols

+ * See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.

+ */

+static

+const struct MQRspec_Capacity mqrspecCapacity[MQRSPEC_VERSION_MAX + 1] = {

+ {0, {0, 0, 0, 0} },

+ {11, {2, 0, 0, 0} },

+ {13, {5, 6, 0, 0} },

+ {15, {6, 8, 0, 0} },

+ {17, {8, 10, 14, 0} }

+};

+

+int MQRspec_getDataLengthBit(int version, enum QRecLevel level)

+{

+ int w;

+ int ecc;

+

+ w = mqrspecCapacity[version].width - 1;

+ ecc = mqrspecCapacity[version].ec[level];

+ if (ecc == 0)

+ return 0;

+ return w * w - 64 - ecc * 8;

+}

+

+int MQRspec_getDataLength(int version, enum QRecLevel level)

+{

+ return (MQRspec_getDataLengthBit(version, level) + 4) / 8;

+}

+

+int MQRspec_getECCLength(int version, enum QRecLevel level)

+{

+ return mqrspecCapacity[version].ec[level];

+}

+

+int MQRspec_getWidth(int version)

+{

+ return mqrspecCapacity[version].width;

+}

+

+/******************************************************************************

+ * Length indicator

+ *****************************************************************************/

+

+/**

+ * See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.

+ */

+static const int lengthTableBits[4][4] = {

+ {3, 4, 5, 6},

+ {0, 3, 4, 5},

+ {0, 0, 4, 5},

+ {0, 0, 3, 4}

+};

+

+int MQRspec_lengthIndicator(enum QRencodeMode mode, int version)

+{

+ return lengthTableBits[mode][version - 1];

+}

+

+int MQRspec_maximumWords(enum QRencodeMode mode, int version)

+{

+ int bits;

+ int words;

+

+ bits = lengthTableBits[mode][version - 1];

+ words = (1 << bits) - 1;

+ if (mode == QR_MODE_KANJI)

+ words *= 2; /* the number of bytes is required */

+

+ return words;

+}

+

+/******************************************************************************

+ * Format information

+ *****************************************************************************/

+

+/* See calcFormatInfo in tests/test_mqrspec.c */

+static const unsigned int formatInfo[4][8] = {

+ {0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3},

+ {0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4},

+ {0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d},

+ {0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba}

+};

+

+/* See Table 10 of Appendix 1. (pp.115) */

+static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {

+ {-1, -1, -1},

+ {0, -1, -1},

+ {1, 2, -1},

+ {3, 4, -1},

+ {5, 6, 7}

+};

+

+unsigned int MQRspec_getFormatInfo(int mask, int version, enum QRecLevel level)

+{

+ int type;

+

+ if (mask < 0 || mask > 3)

+ return 0;

+ if (version <= 0 || version > MQRSPEC_VERSION_MAX)

+ return 0;

+ if (level == QR_ECLEVEL_H)

+ return 0;

+ type = typeTable[version][level];

+ if (type < 0)

+ return 0;

+

+ return formatInfo[mask][type];

+}

+

+/******************************************************************************

+ * Frame

+ *****************************************************************************/

+

+/**

+ * Cache of initial frames.

+ */

+/* C99 says that static storage shall be initialized to a null pointer

+ * by compiler. */

+static unsigned char *frames[MQRSPEC_VERSION_MAX + 1];

+#ifdef HAVE_LIBPTHREAD

+static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;

+#endif

+

+/**

+ * Put a finder pattern.<

Show more